home *** CD-ROM | disk | FTP | other *** search
/ PC Home 119 / PC Home issue 119.iso / Software / Essentials / Useful / MDAC261.exe / sqlnet.cab / instcat.sql < prev    next >
Encoding:
Text File  |  2001-03-31  |  732.7 KB  |  23,847 lines

  1.  
  2. /*
  3. **    INSTCAT.SQL
  4. **    Installs catalog stored procedures on the Microsoft SQL Server.
  5. ** Copyright Microsoft, Inc. 1994 - 2000
  6. ** All Rights Reserved.
  7. */
  8.  
  9. /*
  10. INTERNAL NOTE:
  11. When this file is updated, you MUST change the last row inserted into
  12. spt_server_info to be version number of this file.    the convention is
  13. j.nn.bbb, where j is the major version number ('7' now), nn is the minor
  14. version number ('10' now), and bbb is the build number.
  15. insert into spt_server_info
  16.     values (500, 'SYS_SPROC_VERSION', '8.00.bbb')
  17. */
  18.  
  19. /****************************************************************************/
  20. /* This portion sets up the ability to perform all the functions in this    */
  21. /* script                                                                    */
  22. /****************************************************************************/
  23. go
  24. use master
  25. go
  26. dump tran master with no_log
  27. go
  28.  
  29. set quoted_identifier on
  30. go
  31.  
  32. if (exists (select * from sysobjects
  33.         where name = 'sp_configure' and type = 'P '))
  34. begin
  35.     execute sp_configure 'update',1
  36. end
  37. reconfigure with override
  38. go
  39.  
  40. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  41.  
  42. go
  43.  
  44. /*
  45. ** If old versions of tables exist, drop them.
  46. */
  47. if (exists (select * from sysobjects
  48.         where name = 'MSdatatype_info' and type = 'U '))
  49.     drop table MSdatatype_info
  50. go
  51. if (exists (select * from sysobjects
  52.         where name = 'MSdatatype_info_ext' and type = 'U '))
  53.     drop table MSdatatype_info_ext
  54. go
  55. if (exists (select * from sysobjects
  56.         where name = 'MStable_types' and type = 'U '))
  57.     drop table MStable_types
  58. go
  59. if (exists (select * from sysobjects
  60.         where name = 'MSserver_info' and type = 'U '))
  61.     drop table MSserver_info
  62. go
  63. if (exists (select * from sysobjects
  64.         where name = 'spt_table_types' and type = 'U '))
  65.     drop table spt_table_types
  66. go
  67. /*
  68. ** If tables or procs already exist, drop them.
  69. */
  70.  
  71. if (exists (select * from sysobjects
  72.         where name = 'spt_datatype_info' and type = 'U '))
  73.     drop table spt_datatype_info
  74. go
  75. if (exists (select * from sysobjects
  76.         where name = 'spt_datatype_info_ext' and type = 'U '))
  77.     drop table spt_datatype_info_ext
  78. go
  79. if (exists (select * from sysobjects
  80.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  81.     drop proc sp_add_server_sortinfo
  82. go
  83. if (exists (select * from sysobjects
  84.         where name = 'sp_add_server_sortinfo75' and type = 'P '))
  85.     drop proc sp_add_server_sortinfo75
  86. go
  87. if (exists (select * from sysobjects
  88.         where name = 'spt_server_info' and type = 'U '))
  89.     drop table spt_server_info
  90. go
  91. if (exists (select * from sysobjects
  92.         where name = 'sp_tables' and type = 'P '))
  93.     drop proc sp_tables
  94. go
  95. if (exists (select * from sysobjects
  96.         where name = 'sp_statistics' and type = 'P '))
  97.     drop proc sp_statistics
  98. go
  99. if (exists (select * from sysobjects
  100.         where name = 'sp_columns' and type = 'P '))
  101.     drop proc sp_columns
  102. go
  103. if (exists (select * from sysobjects
  104.         where name = 'sp_fkeys' and type = 'P '))
  105.     drop proc sp_fkeys
  106. go
  107. if (exists (select * from sysobjects
  108.         where name = 'sp_pkeys' and type = 'P '))
  109.     drop proc sp_pkeys
  110. dump tran master with no_log
  111. go
  112.  
  113. go
  114. if (exists (select * from sysobjects
  115.         where name = 'sp_stored_procedures' and type = 'P '))
  116.     drop proc sp_stored_procedures
  117. go
  118. if (exists (select * from sysobjects
  119.         where name = 'sp_sproc_columns' and type = 'P '))
  120.     drop proc sp_sproc_columns
  121. go
  122. if (exists (select * from sysobjects
  123.         where name = 'sp_table_privileges' and type = 'P '))
  124.     drop proc sp_table_privileges
  125. go
  126. if (exists (select * from sysobjects
  127.         where name = 'sp_column_privileges' and type = 'P '))
  128.     drop proc sp_column_privileges
  129. go
  130. dump tran master with no_log
  131. go
  132. if (exists (select * from sysobjects
  133.         where name = 'sp_server_info' and type = 'P '))
  134.     drop proc sp_server_info
  135. go
  136. if (exists (select * from sysobjects
  137.         where name = 'sp_datatype_info' and type = 'P '))
  138.     drop proc sp_datatype_info
  139. go
  140. if (exists (select * from sysobjects
  141.         where name = 'sp_special_columns' and type = 'P '))
  142.     drop proc sp_special_columns
  143. go
  144. if (exists (select * from sysobjects
  145.         where name = 'sp_databases' and type = 'P '))
  146.     drop proc sp_databases
  147. go
  148. dump tran master with no_log
  149. go
  150. if (exists (select * from sysobjects
  151.         where name = 'sp_ddopen' and type = 'P '))
  152.     drop proc sp_ddopen
  153. go
  154. if (exists (select * from sysobjects
  155.         where name = 'sp_tableswc' and type = 'P '))
  156.     drop proc sp_tableswc
  157. go
  158.  
  159. if (exists (select * from sysobjects
  160.         where name = 'sp_tablecollations' and type = 'P'))
  161.     drop proc sp_tablecollations
  162. go
  163.  
  164. if (exists (select * from sysobjects
  165.         where name = 'sp_bcp_dbcmptlevel' and type = 'P'))
  166.     drop proc sp_bcp_dbcmptlevel
  167. go
  168.  
  169. dump tran master with no_log
  170. go
  171.  
  172.  
  173. if (exists (select * from sysobjects
  174.         where name = 'spt_provider_types' and type = 'U '))
  175.     begin
  176.     drop table spt_provider_types
  177.     dump tran master with no_log
  178.     end
  179. go
  180. if (exists (select * from sysobjects
  181.         where name = 'sp_catalogs_rowset' and type = 'P '))
  182.     begin
  183.     drop procedure sp_catalogs_rowset
  184.     dump tran master with no_log
  185.     end
  186. go
  187. if (exists (select * from sysobjects
  188.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  189.     begin
  190.     drop procedure sp_column_privileges_rowset
  191.     dump tran master with no_log
  192.     end
  193. go
  194. if (exists (select * from sysobjects
  195.         where name = 'sp_columns_rowset' and type = 'P '))
  196.     begin
  197.     drop procedure sp_columns_rowset
  198.     dump tran master with no_log
  199.     end
  200. go
  201. if (exists (select * from sysobjects
  202.         where name = 'sp_check_constraints_rowset' and type = 'P '))
  203.     begin
  204.     drop procedure sp_check_constraints_rowset
  205.     dump tran master with no_log
  206.     end
  207. go
  208. if (exists (select * from sysobjects
  209.         where name = 'sp_check_constbytable_rowset' and type = 'P '))
  210.     begin
  211.     drop procedure sp_check_constbytable_rowset
  212.     dump tran master with no_log
  213.     end
  214. go
  215. if (exists (select * from sysobjects
  216.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  217.     begin
  218.     drop procedure sp_foreign_keys_rowset
  219.     dump tran master with no_log
  220.     end
  221. go
  222. if (exists (select * from sysobjects
  223.         where name = 'sp_indexes_rowset' and type = 'P '))
  224.     begin
  225.     drop procedure sp_indexes_rowset
  226.     dump tran master with no_log
  227.     end
  228. go
  229. if (exists (select * from sysobjects
  230.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  231.     begin
  232.     drop procedure sp_primary_keys_rowset
  233.     dump tran master with no_log
  234.     end
  235. go
  236. if (exists (select * from sysobjects
  237.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  238.     begin
  239.     drop procedure sp_procedure_params_rowset
  240.     dump tran master with no_log
  241.     end
  242. go
  243. if (exists (select * from sysobjects
  244.         where name = 'sp_procedures_rowset' and type = 'P '))
  245.     begin
  246.     drop procedure sp_procedures_rowset
  247.     dump tran master with no_log
  248.     end
  249. go
  250. if (exists (select * from sysobjects
  251.         where name = 'sp_provider_types_rowset' and type = 'P '))
  252.     begin
  253.     drop procedure sp_provider_types_rowset
  254.     dump tran master with no_log
  255.     end
  256. go
  257. if (exists (select * from sysobjects
  258.         where name = 'sp_schemata_rowset' and type = 'P '))
  259.     begin
  260.     drop procedure sp_schemata_rowset
  261.     dump tran master with no_log
  262.     end
  263. go
  264. if (exists (select * from sysobjects
  265.         where name = 'sp_statistics_rowset' and type = 'P '))
  266.     begin
  267.     drop procedure sp_statistics_rowset
  268.     dump tran master with no_log
  269.     end
  270. go
  271. if (exists (select * from sysobjects
  272.         where name = 'sp_tables_rowset' and type = 'P '))
  273.     begin
  274.     drop procedure sp_tables_rowset
  275.     dump tran master with no_log
  276.     end
  277. go
  278. if (exists (select * from sysobjects
  279.         where name = 'sp_tables_info_rowset' and type = 'P '))
  280.     begin
  281.     drop procedure sp_tables_info_rowset
  282.     dump tran master with no_log
  283.     end
  284. go
  285. if (exists (select * from sysobjects
  286.         where name = 'sp_table_constraints_rowset' and type = 'P '))
  287.     begin
  288.     drop proc sp_table_constraints_rowset
  289.     dump tran master with no_log
  290.     end
  291. go
  292. if (exists (select * from sysobjects
  293.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  294.     begin
  295.     drop proc sp_table_privileges_rowset
  296.     dump tran master with no_log
  297.     end
  298. go
  299. if (exists (select * from sysobjects
  300.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  301.     begin
  302.     drop proc sp_linkedservers_rowset
  303.     dump tran master with no_log
  304.     end
  305. go
  306.  
  307. if (exists (select * from sysobjects
  308.         where name = 'sp_table_statistics_rowset' and type = 'P '))
  309.     begin
  310.     drop proc sp_table_statistics_rowset
  311.     dump tran master with no_log
  312.     end
  313. go
  314.  
  315. if (exists (select * from sysobjects
  316.         where name = 'sp_oledb_column_constraints' and type = 'P '))
  317.     begin
  318.     drop proc sp_oledb_column_constraints
  319.     dump tran master with no_log
  320.     end
  321. go
  322.  
  323. if (exists (select * from sysobjects
  324.         where name = 'sp_oledb_indexinfo' and type = 'P '))
  325.     begin
  326.     drop proc sp_oledb_indexinfo
  327.     dump tran master with no_log
  328.     end
  329. go
  330.  
  331. if (exists (select * from sysobjects
  332.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  333.     begin
  334.     drop proc sp_oledb_ro_usrname
  335.     dump tran master with no_log
  336.     end
  337. go
  338.  
  339. if (exists (select * from sysobjects
  340.         where name = 'sp_oledb_deflang' and type = 'P '))
  341.     begin
  342.     drop proc sp_oledb_deflang
  343.     dump tran master with no_log
  344.     end
  345. go
  346.  
  347. if (exists (select * from sysobjects
  348.         where name = 'sp_oledb_defdb' and type = 'P '))
  349.     begin
  350.     drop proc sp_oledb_defdb
  351.     dump tran master with no_log
  352.     end
  353. go
  354.  
  355. if (exists (select * from sysobjects
  356.         where name = 'sp_oledb_database' and type = 'P '))
  357.     begin
  358.     drop proc sp_oledb_database
  359.     dump tran master with no_log
  360.     end
  361. go
  362.  
  363. if (exists (select * from sysobjects
  364.         where name = 'sp_oledb_language' and type = 'P '))
  365.     begin
  366.     drop proc sp_oledb_language
  367.     dump tran master with no_log
  368.     end
  369. go
  370.  
  371. print 'creating table spt_datatype_info_ext'
  372. go
  373.  
  374. if (charindex('6.00', @@version) = 0 and
  375.     charindex('6.50', @@version) = 0 and
  376.     charindex('7.00', @@version) = 0 and
  377.     charindex('8.00', @@version) = 0)
  378. begin    /*    Pre 6.0 server */
  379.     print ''
  380.     print ''
  381.     print 'Warning:'
  382.     print 'you are installing the stored procedures '
  383.     print 'on a pre 6.0 SQL Server.'
  384.     print 'Ignore the following error.'
  385.     create table spt_datatype_info_ext (
  386.                 user_type        smallint    not null,
  387.                 CREATE_PARAMS    varchar(32) null,
  388.                 AUTO_INCREMENT    smallint null,
  389.                 typename        varchar(32))
  390. end
  391. go
  392. if (charindex('6.00', @@version) > 0 or
  393.     charindex('6.50', @@version) > 0 or
  394.     charindex('7.00', @@version) > 0 or
  395.     charindex('8.00', @@version) > 0)
  396. begin    /* 6.0 or later server */
  397.     create table spt_datatype_info_ext (
  398.                 user_type        smallint    not null,
  399.                 CREATE_PARAMS    varchar(32) null,
  400.                 AUTO_INCREMENT    smallint null,
  401.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  402. end
  403. go
  404.  
  405. grant select on spt_datatype_info_ext to public
  406. go
  407.  
  408.  
  409. insert into spt_datatype_info_ext
  410.     /* CHAR      user_type, create_params, auto_increment */
  411.     values             (1,    'length' ,0, 'char')
  412.  
  413. insert into spt_datatype_info_ext
  414.     /* VARCHAR     user_type, create_params, auto_increment */
  415.     values             (2,    'max length' ,0, 'varchar')
  416.  
  417. insert into spt_datatype_info_ext
  418.     /* BINARY     user_type, create_params, auto_increment */
  419.     values             (3,    'length' ,0, 'binary')
  420.  
  421. insert into spt_datatype_info_ext
  422.     /* VARBINARY user_type, create_params, auto_increment */
  423.     values             (4,    'max length' ,0, 'varbinary')
  424.  
  425. if    (charindex('6.00', @@version) > 0 or
  426.      charindex('6.50', @@version) > 0 or
  427.      charindex('7.00', @@version) > 0 or
  428.      charindex('8.00', @@version) > 0)
  429. begin    /*    Add 6.0 data types */
  430.     insert into spt_datatype_info_ext
  431.         /* DECIMAL user_type, create_params, auto_increment */
  432.         values             (26,    'precision,scale' ,0, 'decimal')
  433.  
  434.     insert into spt_datatype_info_ext
  435.         /* NUMERIC user_type, create_params, auto_increment */
  436.         values             (25,    'precision,scale' ,0, 'numeric')
  437.  
  438.     insert into spt_datatype_info_ext
  439.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  440.         values             (26,    'precision' ,1, 'decimal')
  441.  
  442.     insert into spt_datatype_info_ext
  443.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  444.         values             (25,    'precision' ,1, 'numeric')
  445.  
  446. end
  447. else    /*    Pre 6.0 server, add SYSNAME create param */
  448.     begin
  449.         insert into spt_datatype_info_ext
  450.             /* SYSNAME     user_type, create_param, auto_increments */
  451.             values             (18,    'max length' ,0, 'sysname')
  452.  
  453.     end
  454. go
  455.  
  456. if (charindex('7.00', @@version) = 0 and
  457.     charindex('8.00', @@version) = 0)
  458. begin
  459.     print ''
  460.     print ''
  461.     print 'Warning:'
  462.     print 'you are installing the stored procedures '
  463.     print 'on a pre 8.0 SQL Server.'
  464.     print 'Ignore the following errors.'
  465. end
  466. go
  467. if (charindex('7.00', @@version) > 0 or
  468.     charindex('8.00', @@version) > 0)
  469. begin    /*    Update usertypes for 8.0 server */
  470.     begin tran
  471.     insert into spt_datatype_info_ext
  472.         /* NCHAR     user_type, create_params, auto_increment */
  473.         values             (0,    'length' ,0, 'nchar')
  474.  
  475.     insert into spt_datatype_info_ext
  476.         /* NVARCHAR     user_type, create_params, auto_increment */
  477.         values             (0,    'max length' ,0, 'nvarchar')
  478.  
  479.     /* SET user_type TO SPHINX VALUES */
  480.     update spt_datatype_info_ext set user_type = xusertype
  481.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  482.  
  483.     commit tran
  484. end
  485. go
  486.  
  487. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  488. go
  489.  
  490. print 'creating table spt_datatype_info'
  491. go
  492. if (charindex('6.00', @@version) = 0 and
  493.     charindex('6.50', @@version) = 0 and
  494.     charindex('7.00', @@version) = 0 and
  495.     charindex('8.00', @@version) = 0)
  496. begin    /*    Pre 6.0 server */
  497.     print ''
  498.     print ''
  499.     print 'Warning:'
  500.     print 'you are installing the stored procedures '
  501.     print 'on a pre 6.0 SQL Server.'
  502.     print 'Ignore the following error.'
  503.     create table spt_datatype_info (
  504.         ss_dtype            tinyint     not null,
  505.         fixlen                int         null,        /* datatype len for variable, else null */
  506.         ODBCVer             tinyint     null,        /* version if needed, else null */
  507.         TYPE_NAME            varchar(32)    not null,
  508.         DATA_TYPE            smallint    not null,
  509.         data_precision        int         null,
  510.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  511.         RADIX                smallint    null,
  512.         length                int         null,
  513.         LITERAL_PREFIX        varchar(32) null,
  514.         LITERAL_SUFFIX        varchar(32) null,
  515.         CREATE_PARAMS        varchar(32) null,
  516.         NULLABLE            smallint    not null,
  517.         CASE_SENSITIVE        smallint    not null,
  518.         SEARCHABLE            smallint    not null,
  519.         UNSIGNED_ATTRIBUTE    smallint    null,
  520.         MONEY                smallint    not null,
  521.         AUTO_INCREMENT        smallint    null,
  522.         LOCAL_TYPE_NAME     varchar(32)    null,
  523.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  524.         SQL_DATA_TYPE        smallint    not null,
  525.         SQL_DATETIME_SUB    smallint    null)
  526. end
  527. go
  528. if (charindex('6.00', @@version) > 0 or
  529.     charindex('6.50', @@version) > 0 or
  530.     charindex('7.00', @@version) > 0 or
  531.     charindex('8.00', @@version) > 0)
  532. begin    /* 6.0 or later server */
  533.     create table spt_datatype_info (
  534.         ss_dtype            tinyint     not null,
  535.         fixlen                int         null,        /* datatype len for variable, else null */
  536.         ODBCVer             tinyint     null,        /* version if needed, else null */
  537.         TYPE_NAME            sysname     not null,
  538.         DATA_TYPE            smallint    not null,
  539.         data_precision        int         null,
  540.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  541.         RADIX                smallint    null,
  542.         length                int         null,
  543.         LITERAL_PREFIX        varchar(32) null,
  544.         LITERAL_SUFFIX        varchar(32) null,
  545.         CREATE_PARAMS        varchar(32) null,
  546.         NULLABLE            smallint    not null,
  547.         CASE_SENSITIVE        smallint    not null,
  548.         SEARCHABLE            smallint    not null,
  549.         UNSIGNED_ATTRIBUTE    smallint    null,
  550.         MONEY                smallint    not null,
  551.         AUTO_INCREMENT        smallint    null,
  552.         LOCAL_TYPE_NAME     sysname     null,
  553.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  554.         SQL_DATA_TYPE        smallint    not null,
  555.         SQL_DATETIME_SUB    smallint    null)
  556. end
  557. go
  558.  
  559. grant select on spt_datatype_info to public
  560. go
  561.  
  562. /* Get case sensitivity */
  563. if 'A' = 'A' /* create dummy begin block */
  564. begin
  565.     declare @case smallint
  566.  
  567.     begin tran
  568.     select @case = 0
  569.     select @case = 1 where 'a' <> 'A'
  570.  
  571.     /* Local Binary */
  572.     insert into spt_datatype_info values
  573.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  574.  
  575.     /* Local Bit */
  576.     insert into spt_datatype_info values
  577.     (50,null,null,'bit',-7,1,0,null,1,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  578.  
  579.     /* Local Char */
  580.     insert into spt_datatype_info values
  581.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  582.  
  583.     /* Local Datetime */
  584.     insert into spt_datatype_info values
  585.     (61,8,2,'datetime',11,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  586.     insert into spt_datatype_info values
  587.     (61,8,3,'datetime',93,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  588.  
  589.     /* Local Smalldatetime */
  590.     insert into spt_datatype_info values
  591.     (58,4,2,'smalldatetime',11,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  592.     insert into spt_datatype_info values
  593.     (58,4,3,'smalldatetime',93,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  594.  
  595.     /* Local Float */
  596.     insert into spt_datatype_info values
  597.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  598.     insert into spt_datatype_info values
  599.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  600.  
  601.     /* Local Real */
  602.     insert into spt_datatype_info values
  603.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  604.     insert into spt_datatype_info values
  605.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  606.  
  607.     /* Local Smallmoney */
  608.     insert into spt_datatype_info values
  609.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  610.  
  611.     /* Local Money */
  612.     insert into spt_datatype_info values
  613.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  614.  
  615.     /* Local Int */
  616.     insert into spt_datatype_info values
  617.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  618.  
  619.     commit tran
  620. end
  621. go
  622.  
  623. if 'A' = 'A' /* create dummy begin block */
  624. begin
  625.     declare @case smallint
  626.  
  627.     begin tran
  628.     select @case = 0
  629.     select @case = 1 where 'a' <> 'A'
  630.  
  631.  
  632.     /* Local Smallint */
  633.     insert into spt_datatype_info values
  634.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  635.     insert into spt_datatype_info values
  636.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  637.  
  638.     /* Local Tinyint */
  639.     insert into spt_datatype_info values
  640.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  641.  
  642.     /* Local Text */
  643.     insert into spt_datatype_info values
  644.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  645.  
  646.     /* Local Varbinary */
  647.     insert into spt_datatype_info values
  648.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  649.  
  650.     /* Local Varchar */
  651.     insert into spt_datatype_info values
  652.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  653.  
  654.     /* Local Image */
  655.     insert into spt_datatype_info values
  656.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  657.  
  658.     if (charindex('6.00', @@version) > 0 or
  659.         charindex('6.50', @@version) > 0 or
  660.         charindex('7.00', @@version) > 0 or
  661.         charindex('8.00', @@version) > 0)
  662.     begin    /*    Add 6.0 data types */
  663.         /* Local Decimal */
  664.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  665.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  666.  
  667.         /* Local Numeric */
  668.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  669.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  670.  
  671.         /* Identity attribute data types */
  672.  
  673.         /* Identity Int */
  674.         insert into spt_datatype_info values
  675.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  676.  
  677.         /* Identity Smallint */
  678.         insert into spt_datatype_info values
  679.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  680.  
  681.         /* Identity Tinyint */
  682.         insert into spt_datatype_info values
  683.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  684.  
  685.         /* Identity Numeric */
  686.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  687.         (55,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  688.  
  689.         /* Identity Numeric */
  690.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  691.         (63,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  692.  
  693.     end
  694.  
  695.     if (charindex('7.00', @@version) = 0 and
  696.         charindex('8.00', @@version) = 0)
  697.     begin    /*    Add nullable type for non-8.0 server */
  698.         /* Local Datetimn */
  699.         insert into spt_datatype_info values
  700.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  701.         insert into spt_datatype_info values
  702.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  703.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  704.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  705.         insert into spt_datatype_info values
  706.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  707.  
  708.         /* Local Floatn */
  709.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  710.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  711.         insert into spt_datatype_info values
  712.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  713.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  714.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  715.         insert into spt_datatype_info values
  716.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  717.  
  718.         /* Local Moneyn */
  719.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  720.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  721.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  722.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  723.  
  724.         /* Local Intn */
  725.         insert into spt_datatype_info values /* sql server type is 'intn' */
  726.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  727.         insert into spt_datatype_info values /* sql server type is 'intn' */
  728.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  729.         insert into spt_datatype_info values
  730.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  731.  
  732.         if (charindex('6.00', @@version) > 0 or
  733.             charindex('6.50', @@version) > 0 or
  734.             charindex('7.00', @@version) > 0 or
  735.             charindex('8.00', @@version) > 0)
  736.         begin    /*    Add 6.0 data types */
  737.             /* Local Decimaln */
  738.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  739.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  740.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  741.             (106,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  742.  
  743.             /* Local Numericn */
  744.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  745.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  746.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  747.             (108,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  748.         end
  749.     end
  750.  
  751.     commit tran
  752. end
  753. go
  754.  
  755. if (charindex('7.00', @@version) = 0 and
  756.     charindex('8.00', @@version) = 0)
  757. begin
  758.     print ''
  759.     print ''
  760.     print 'Warning:'
  761.     print 'you are installing the stored procedures '
  762.     print 'on a pre 8.0 SQL Server.'
  763.     print 'Ignore the following errors.'
  764. end
  765. go
  766. if (charindex('7.00', @@version) > 0 or
  767.     charindex('8.00', @@version) > 0)
  768.     begin
  769.         declare @ncase smallint
  770.  
  771.         select @ncase = 0
  772.         select @ncase = 1 where N'a' <> N'A'
  773.  
  774.         /* Local Timestamp */
  775.         insert into spt_datatype_info values
  776.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  777.  
  778.         /* Local GUID */
  779.         insert into spt_datatype_info values
  780.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  781.  
  782.         /* Local NChar */
  783.         insert into spt_datatype_info values
  784.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@ncase,3,null,0,null,'nchar',0,-8,null)
  785.  
  786.         /* Local NVarchar */
  787.         insert into spt_datatype_info values
  788.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@ncase,3,null,0,null,'nvarchar',0,-9,null)
  789.  
  790.         /* Local NText */
  791.         insert into spt_datatype_info values
  792.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@ncase,1,null,0,null,'ntext',0,-10,null)
  793.  
  794.         if (charindex('8.00', @@version) > 0)
  795.         begin
  796. --            /* Local BIGINT */
  797.             insert into spt_datatype_info values
  798.             (127,8,null,'bigint',-5,19,0,10,null,null,null,null,1,0,2,0,0,0,'bigint',null,-5,null)
  799. --            /* Identity BIGINT */
  800.             insert into spt_datatype_info values
  801.             (127,8,null,'bigint identity',-5,19,0,10,null,null,null,null,0,0,2,0,0,1,'bigint identity',null,-5,null)
  802.  
  803.  
  804. --            /* sql_variant */
  805.             insert into spt_datatype_info values
  806.             (
  807.             98,                    --ss_dtype        
  808.             null,                --fixlen
  809.             null,                --ODBCVer
  810.             'sql_variant',        --TYPE_NAME
  811.             -150,                --SQL DATA TYPE
  812.             8000,                --data_precision
  813.             0,                    --numeric_scale
  814.             10,                    --RADIX
  815.             8000,                --length
  816.             null,                --PREFIX
  817.             null,                --SUFFIX
  818.             null,                --Create Params
  819.             1,                    --Nullable
  820.             0,                --Case sensitive
  821.             2,                    --Searchable
  822.             null,                --UNSIGNED_ATTRIBUTE
  823.             0,                --MONEY
  824.             null,                --AUTO_INCREMENT
  825.             'sql_variant',        --LOCAL TYPE NAME
  826.             0,                    --CHARBIN
  827.             -150,                --SQL_DATA_TYPE
  828.             null                --SQL_DATETIME_SUB
  829.             )
  830.         
  831.         end
  832.  
  833.  
  834.  
  835.         update spt_datatype_info set NULLABLE = 1
  836.             where TYPE_NAME = 'bit'
  837.  
  838.         update spt_datatype_info set ss_dtype = isnull((select distinct xtype from systypes
  839.             where TYPE_NAME like name+'%'),0)
  840.     end
  841. go
  842.  
  843. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  844. go
  845.  
  846. dump tran master with no_log
  847. go
  848.  
  849. print 'creating table spt_server_info'
  850. go
  851. create table spt_server_info (
  852.               attribute_id        int NOT NULL,
  853.               attribute_name    varchar(60) NOT NULL,
  854.               attribute_value    varchar(255) NOT NULL)
  855. go
  856.  
  857. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  858. go
  859.  
  860. if (charindex('7.00', @@version) = 0 and charindex('8.00', @@version) = 0)
  861.     begin
  862.         drop procedure sp_add_server_sortinfo    /* not used by other servers */
  863.         drop procedure sp_add_server_sortinfo75    /* not used by older servers */
  864.         dump tran master with no_log
  865.     end
  866. go
  867.  
  868.  
  869. insert into spt_server_info
  870.     values (1,'DBMS_NAME','Microsoft SQL Server')
  871. insert into spt_server_info
  872.     values (2,'DBMS_VER',@@version)
  873. insert into spt_server_info
  874.     values (10,'OWNER_TERM','owner')
  875. insert into spt_server_info
  876.     values (11,'TABLE_TERM','table')
  877. insert into spt_server_info
  878.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  879. insert into spt_server_info
  880.     values (13,'TABLE_LENGTH','30')
  881. insert into spt_server_info
  882.     values (14,'MAX_QUAL_LENGTH','30')
  883. insert into spt_server_info
  884.     values (15,'COLUMN_LENGTH','30')
  885. if 'A' = 'a' /* If not case sensitive server */
  886. begin
  887.     insert into spt_server_info
  888.         values (16,'IDENTIFIER_CASE','MIXED')
  889. end
  890. else
  891. begin
  892.     insert into spt_server_info
  893.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  894. end
  895. insert into spt_server_info
  896.     values (17,'TX_ISOLATION','2')
  897. if (charindex('6.00', @@version) > 0 or
  898.     charindex('6.50', @@version) > 0 or
  899.     charindex('7.00', @@version) > 0) 
  900. begin    /*    Add 6.0 collation sequence */
  901.     insert into spt_server_info
  902.         select 18,'COLLATION_SEQ',
  903.             'charset='+t2.name+' sort_order='+t1.name
  904.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  905.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  906.         from syscharsets t1, syscharsets t2, sysconfigures t3
  907.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  908. end
  909. else 
  910. begin    /*    Add 4.2x collation sequence */
  911.     insert into spt_server_info
  912.         select 18,'COLLATION_SEQ',
  913.             'charset='+t2.name+' sort_order='+t1.name
  914.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  915.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  916.         from syscharsets t1, syscharsets t2, sysconfigures t3
  917.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  918. end
  919. insert into spt_server_info
  920.     values (19,'SAVEPOINT_SUPPORT','Y')
  921. insert into spt_server_info
  922.     values (20,'MULTI_RESULT_SETS','Y')
  923. insert into spt_server_info
  924.     values (22,'ACCESSIBLE_TABLES','Y')
  925. go
  926.  
  927. insert into spt_server_info
  928.     values (100,'USERID_LENGTH','30')
  929. insert into spt_server_info
  930.     values (101,'QUALIFIER_TERM','database')
  931. insert into spt_server_info
  932.     values (102,'NAMED_TRANSACTIONS','Y')
  933. insert into spt_server_info
  934.     values (103,'SPROC_AS_LANGUAGE','Y')
  935. insert into spt_server_info
  936.     values (104,'ACCESSIBLE_SPROC','Y')
  937. insert into spt_server_info
  938.     values (105,'MAX_INDEX_COLS','16')
  939. insert into spt_server_info
  940.     values (106,'RENAME_TABLE','Y')
  941. insert into spt_server_info
  942.     values (107,'RENAME_COLUMN','Y')
  943.  
  944. if (charindex('8.00', @@version) > 0)
  945. begin
  946.     /* Columns may be dropped on 8.0 or later */
  947.     insert into spt_server_info
  948.         values (108,'DROP_COLUMN','Y')
  949. end
  950. else
  951. begin
  952.     insert into spt_server_info
  953.         values (108,'DROP_COLUMN','N')
  954. end
  955.  
  956. if (charindex('8.00', @@version) > 0)
  957. begin
  958.     /* Columns size may be changed on 8.0 or later */
  959.     insert into spt_server_info
  960.         values (109,'INCREASE_COLUMN_LENGTH','Y')
  961. end
  962. else
  963. begin
  964.     insert into spt_server_info
  965.         values (109,'INCREASE_COLUMN_LENGTH','N')
  966. end
  967.  
  968. if (charindex('6.50', @@version) = 0 and
  969.     charindex('7.00', @@version) = 0 and
  970.     charindex('8.00', @@version) = 0)
  971. begin
  972.     insert into spt_server_info
  973.         values (110,'DDL_IN_TRANSACTION','N')
  974. end
  975. else
  976. begin
  977.     insert into spt_server_info
  978.         values (110,'DDL_IN_TRANSACTION','Y')
  979. end
  980.  
  981.  
  982. if (charindex('8.00', @@version) > 0)
  983. begin
  984.     /* Descending indexes allowed on 8.0 or later */
  985.     insert into spt_server_info
  986.         values (111,'DESCENDING_INDEXES','Y')
  987. end
  988. else
  989. begin
  990.     insert into spt_server_info
  991.         values (111,'DESCENDING_INDEXES','N')
  992. end
  993.  
  994. insert into spt_server_info
  995.     values (112,'SP_RENAME','Y')
  996. insert into spt_server_info
  997.     values (113,'REMOTE_SPROC','Y')
  998. insert into spt_server_info
  999.     values (500,'SYS_SPROC_VERSION','8.00.375')
  1000. go
  1001.  
  1002. if (charindex('7.00', @@version) > 0 or
  1003.     charindex('8.00', @@version) > 0)
  1004. begin    /*    Update values for 8.0 server */
  1005.     update spt_server_info set attribute_value = '128'
  1006.         where attribute_id in (12,13,14,15,100)
  1007. end
  1008. go
  1009.  
  1010. grant select on spt_server_info to public
  1011. go
  1012.  
  1013. print 'creating sp_column_privileges'
  1014. go
  1015.  
  1016. /*    Procedure for pre 6.50 server */
  1017. CREATE PROCEDURE sp_column_privileges (
  1018.             @table_name         varchar(32),
  1019.             @table_owner        varchar(32) = null,
  1020.             @table_qualifier    varchar(32) = null,
  1021.             @column_name        varchar(96) = null)    /* 3*32 */
  1022. as
  1023.  
  1024.     set nocount on
  1025.     declare @table_id     int
  1026.     DECLARE @full_table_name    varchar(65)    /* 2*32 + 1 */
  1027.     declare @low smallint                     /* range of userids to check */
  1028.     declare @high smallint
  1029.     declare @owner_uid smallint
  1030.  
  1031.     select @low = 0, @high = 32767
  1032.  
  1033.     if @column_name is null /*    If column name not supplied, match all */
  1034.         select @column_name = '%'
  1035.  
  1036.     if @table_qualifier is not null
  1037.     begin
  1038.         if db_name() <> @table_qualifier
  1039.         begin    /* If qualifier doesn't match current database */
  1040.             raiserror 20001 '~~Rush_5~~'
  1041.             return
  1042.         end
  1043.     end
  1044.     if @table_owner is null
  1045.     begin    /* If unqualified table name */
  1046.         SELECT @full_table_name = @table_name
  1047.     end
  1048.     else
  1049.     begin    /* Qualified table name */
  1050.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1051.     end
  1052.     /*    Get Object ID */
  1053.     select @table_id = object_id(@full_table_name)
  1054.  
  1055.     if (@@trancount <> 0)
  1056.     begin    /* If inside a transaction */
  1057.         raiserror 20003 '~~Rush_6~~'
  1058.         return
  1059.     end
  1060.  
  1061.     /*
  1062.     ** We need to create a table which will contain a row for every row to
  1063.     ** be returned to the client.
  1064.     */
  1065.  
  1066.     create table #column_priv1(
  1067.         COLUMN_NAME             varchar(32) NOT NULL,
  1068.         grantor                 smallint NOT NULL,
  1069.         grantee                 smallint NOT NULL,
  1070.         select_privilege        bit,
  1071.         select_grantable        bit,
  1072.         insert_privilege        bit,
  1073.         insert_grantable        bit,
  1074.         update_privilege        bit,
  1075.         update_grantable        bit,
  1076.         references_privilege    bit,
  1077.         references_grantable    bit,
  1078.         uid                     smallint NOT NULL,
  1079.         gid                     smallint NOT NULL)
  1080.  
  1081. /*
  1082. ** insert a row for the table owner (who has all permissions)
  1083. */
  1084.     select @owner_uid = (
  1085.         select uid
  1086.         from sysobjects
  1087.         where id = @table_id)
  1088.  
  1089.     if (charindex('6.00', @@version) > 0)
  1090.     begin
  1091.         insert into #column_priv1
  1092.             select
  1093.                 c.name,
  1094.                 u.uid,
  1095.                 @owner_uid,
  1096.                 0,
  1097.                 1,
  1098.                 0,
  1099.                 1,
  1100.                 0,
  1101.                 1,
  1102.                 0,
  1103.                 1,
  1104.                 @owner_uid,
  1105.                 0
  1106.             from syscolumns c, sysusers u
  1107.             where id = @table_id
  1108.                 and c.number = 0
  1109.                 and u.uid = 1        /* grantor is dbo of database */
  1110.     end
  1111.     else
  1112.     begin
  1113.         insert into #column_priv1
  1114.             select
  1115.                 c.name,
  1116.                 u.uid,
  1117.                 @owner_uid,
  1118.                 0,
  1119.                 1,
  1120.                 0,
  1121.                 1,
  1122.                 0,
  1123.                 1,
  1124.                 0,
  1125.                 0,
  1126.                 @owner_uid,
  1127.                 0
  1128.             from syscolumns c, sysusers u
  1129.             where id = @table_id
  1130.                 and c.number = 0
  1131.                 and u.uid = 1        /* grantor is dbo of database */
  1132.     end
  1133. /*
  1134. ** now stick in a row for every column for every user in the database
  1135. ** we will need to weed out those who have no permissions later
  1136. ** (and yes this is a cartesion product: the uid field in sysprotects
  1137. ** can also have a group id, in which case we need to extend those
  1138. ** privileges to all group members).
  1139. */
  1140.  
  1141.     insert into #column_priv1
  1142.         select distinct
  1143.             c.name,
  1144.             o.uid,
  1145.             u.uid,
  1146.             0,
  1147.             0,
  1148.             0,
  1149.             0,
  1150.             0,
  1151.             0,
  1152.             0,
  1153.             0,
  1154.             u.uid,
  1155.             u.gid
  1156.         from sysusers u, syscolumns c, sysobjects o
  1157.         where o.id = @table_id
  1158.             and c.id = o.id
  1159.             and c.number = 0
  1160.             and u.gid <> u.uid
  1161.             and u.uid <> @owner_uid
  1162.  
  1163.     /*
  1164.     ** we need to create another temporary table to contain all the various
  1165.     ** protection information for the table in question
  1166.     */
  1167.     create table #protects (
  1168.                 uid         smallint NOT NULL,
  1169.                 grantor        smallint NOT NULL,
  1170.                 action        tinyint NOT NULL,
  1171.                 protecttype tinyint NOT NULL,
  1172.                 name        varchar(32) NOT NULL)
  1173.  
  1174.     insert into #protects
  1175.         select
  1176.             p.uid,
  1177.             p.uid,
  1178.             p.action,
  1179.             p.protecttype,
  1180.             isnull(col_name(id, c.number), '~All')
  1181.             from
  1182.                 sysprotects p,
  1183.                 master.dbo.spt_values c,
  1184.                 master.dbo.spt_values a,
  1185.                 master.dbo.spt_values b
  1186.             where
  1187.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1188.                     & c.high <> 0
  1189.                     and c.number <= (
  1190.                         select count(*)
  1191.                         from syscolumns
  1192.                         where id = @table_id)
  1193.                 and c.type = 'P'
  1194.                 and a.type = 'T'
  1195.                 and a.number = p.action
  1196.                 and p.action in (193,195,197,26)
  1197.                 and b.type = 'T'
  1198.                 and b.number = p.protecttype
  1199.                 and p.id = @table_id
  1200.                 and p.uid between @low and @high
  1201.  
  1202.  
  1203.     update #column_priv1
  1204.     set select_privilege = 1
  1205.         from #protects p
  1206.     where
  1207.         p.protecttype = 205
  1208.         and p.action = 193
  1209.         and (p.name = #column_priv1.COLUMN_NAME
  1210.             or name = '~All')
  1211.         and (p.uid = 0
  1212.             or p.uid = #column_priv1.gid
  1213.             or p.uid = #column_priv1.uid)
  1214.         and not exists (
  1215.             select * from #protects
  1216.             where
  1217.                 protecttype = 206
  1218.                 and action = 193
  1219.                 and (name = #column_priv1.COLUMN_NAME
  1220.                     or name = '~All')
  1221.                 and ( uid = 0
  1222.                     or uid = #column_priv1.gid
  1223.                     or uid = #column_priv1.uid))
  1224.  
  1225.     update #column_priv1
  1226.     set insert_privilege = 1
  1227.         from #protects p
  1228.     where
  1229.         p.protecttype = 205
  1230.         and p.action = 195
  1231.         and (p.name = #column_priv1.COLUMN_NAME
  1232.             or name = '~All')
  1233.         and (p.uid = 0
  1234.             or p.uid = #column_priv1.gid
  1235.             or p.uid = #column_priv1.uid)
  1236.         and not exists (
  1237.             select * from #protects
  1238.             where
  1239.                 protecttype = 206
  1240.                 and action = 195
  1241.                 and (name = #column_priv1.COLUMN_NAME
  1242.                        or name = '~All')
  1243.                 and (uid = 0
  1244.                     or uid = #column_priv1.gid
  1245.                     or uid = #column_priv1.uid))
  1246.  
  1247.     update #column_priv1
  1248.     set update_privilege = 1
  1249.         from #protects p
  1250.     where
  1251.         p.protecttype = 205
  1252.         and p.action = 197
  1253.         and (p.name = #column_priv1.COLUMN_NAME
  1254.             or name = '~All')
  1255.         and (p.uid = 0
  1256.             or p.uid = #column_priv1.gid
  1257.             or p.uid = #column_priv1.uid)
  1258.         and not exists (
  1259.             select * from #protects
  1260.                 where protecttype = 206
  1261.                 and action = 197
  1262.                 and (name = #column_priv1.COLUMN_NAME
  1263.                     or name = '~All')
  1264.                 and (uid = 0
  1265.                     or uid = #column_priv1.gid
  1266.                     or uid = #column_priv1.uid))
  1267.  
  1268.     update #column_priv1
  1269.     set references_privilege = 1
  1270.         from #protects p
  1271.     where
  1272.         p.protecttype = 205
  1273.         and p.action = 26
  1274.         and (p.name = #column_priv1.COLUMN_NAME
  1275.             or name = '~All')
  1276.         and (p.uid = 0
  1277.             or p.uid = #column_priv1.gid
  1278.             or p.uid = #column_priv1.uid)
  1279.         and not exists (
  1280.             select * from #protects
  1281.                 where protecttype = 206
  1282.                 and action = 26
  1283.                 and (name = #column_priv1.COLUMN_NAME
  1284.                     or name = '~All')
  1285.                 and (uid = 0
  1286.                     or uid = #column_priv1.gid
  1287.                     or uid = #column_priv1.uid))
  1288.  
  1289.     update #column_priv1
  1290.     set select_grantable = 1
  1291.         from #protects p
  1292.     where
  1293.         p.protecttype = 204
  1294.         and p.action = 193
  1295.         and (p.name = #column_priv1.COLUMN_NAME
  1296.             or name = '~All')
  1297.         and (p.uid = 0
  1298.             or p.uid = #column_priv1.gid
  1299.             or p.uid = #column_priv1.uid)
  1300.         and not exists (
  1301.             select * from #protects
  1302.             where
  1303.                 protecttype = 206
  1304.                 and action = 193
  1305.                 and (name = #column_priv1.COLUMN_NAME
  1306.                     or name = '~All')
  1307.                 and ( uid = 0
  1308.                     or uid = #column_priv1.gid
  1309.                     or uid = #column_priv1.uid))
  1310.  
  1311.     update #column_priv1
  1312.     set insert_grantable = 1
  1313.         from #protects p
  1314.     where
  1315.         p.protecttype = 204
  1316.         and p.action = 195
  1317.         and (p.name = #column_priv1.COLUMN_NAME
  1318.             or name = '~All')
  1319.         and (p.uid = 0
  1320.             or p.uid = #column_priv1.gid
  1321.             or p.uid = #column_priv1.uid)
  1322.         and not exists (
  1323.             select * from #protects
  1324.             where
  1325.                 protecttype = 206
  1326.                 and action = 195
  1327.                 and (name = #column_priv1.COLUMN_NAME
  1328.                     or name = '~All')
  1329.                 and ( uid = 0
  1330.                     or uid = #column_priv1.gid
  1331.                     or uid = #column_priv1.uid))
  1332.  
  1333.     update #column_priv1
  1334.     set update_grantable = 1
  1335.         from #protects p
  1336.     where
  1337.         p.protecttype = 204
  1338.         and p.action = 197
  1339.         and (p.name = #column_priv1.COLUMN_NAME
  1340.             or name = '~All')
  1341.         and (p.uid = 0
  1342.             or p.uid = #column_priv1.gid
  1343.             or p.uid = #column_priv1.uid)
  1344.         and not exists (
  1345.             select * from #protects
  1346.             where
  1347.                 protecttype = 206
  1348.                 and action = 197
  1349.                 and (name = #column_priv1.COLUMN_NAME
  1350.                     or name = '~All')
  1351.                 and ( uid = 0
  1352.                     or uid = #column_priv1.gid
  1353.                     or uid = #column_priv1.uid))
  1354.  
  1355.     update #column_priv1
  1356.     set references_grantable = 1
  1357.         from #protects p
  1358.     where
  1359.         p.protecttype = 204
  1360.         and p.action = 26
  1361.         and (p.name = #column_priv1.COLUMN_NAME
  1362.             or name = '~All')
  1363.         and (p.uid = 0
  1364.             or p.uid = #column_priv1.gid
  1365.             or p.uid = #column_priv1.uid)
  1366.         and not exists (
  1367.             select * from #protects
  1368.             where
  1369.                 protecttype = 206
  1370.                 and action = 26
  1371.                 and (name = #column_priv1.COLUMN_NAME
  1372.                     or name = '~All')
  1373.                 and ( uid = 0
  1374.                     or uid = #column_priv1.gid
  1375.                     or uid = #column_priv1.uid))
  1376.  
  1377.     create table #column_priv2(
  1378.         COLUMN_NAME     varchar(32) NOT NULL,
  1379.         grantor         smallint NULL,
  1380.         grantee         smallint NOT NULL,
  1381.         PRIVILEGE        varchar(32) NOT NULL,
  1382.         IS_GRANTABLE    varchar(3) NULL)
  1383.  
  1384.     insert into #column_priv2
  1385.         select
  1386.             COLUMN_NAME,
  1387.             grantor,
  1388.             grantee,
  1389.             'SELECT',
  1390.             'NO'
  1391.         from #column_priv1
  1392.         where select_privilege = 1 and select_grantable    = 0
  1393.  
  1394.     insert into #column_priv2
  1395.         select
  1396.             COLUMN_NAME,
  1397.             grantor,
  1398.             grantee,
  1399.             'INSERT',
  1400.             'NO'
  1401.         from #column_priv1
  1402.         where insert_privilege = 1 and insert_grantable = 0
  1403.  
  1404.     insert into #column_priv2
  1405.         select
  1406.             COLUMN_NAME,
  1407.             grantor,
  1408.             grantee,
  1409.             'UPDATE',
  1410.             'NO'
  1411.         from #column_priv1
  1412.         where update_privilege = 1 and update_grantable = 0
  1413.  
  1414.     insert into #column_priv2
  1415.         select
  1416.             COLUMN_NAME,
  1417.             grantor,
  1418.             grantee,
  1419.             'REFERENCES',
  1420.             'NO'
  1421.         from #column_priv1
  1422.         where references_privilege = 1 and references_grantable = 0
  1423.  
  1424.     insert into #column_priv2
  1425.         select
  1426.             COLUMN_NAME,
  1427.             grantor,
  1428.             grantee,
  1429.             'SELECT',
  1430.             'YES'
  1431.         from #column_priv1
  1432.         where select_grantable = 1
  1433.  
  1434.     insert into #column_priv2
  1435.         select
  1436.             COLUMN_NAME,
  1437.             grantor,
  1438.             grantee,
  1439.             'INSERT',
  1440.             'YES'
  1441.         from #column_priv1
  1442.         where insert_grantable = 1
  1443.  
  1444.     insert into #column_priv2
  1445.         select
  1446.             COLUMN_NAME,
  1447.             grantor,
  1448.             grantee,
  1449.             'UPDATE',
  1450.             'YES'
  1451.         from #column_priv1
  1452.         where update_grantable = 1
  1453.  
  1454.     insert into #column_priv2
  1455.         select
  1456.             COLUMN_NAME,
  1457.             grantor,
  1458.             grantee,
  1459.             'REFERENCES',
  1460.             'YES'
  1461.         from #column_priv1
  1462.         where references_grantable = 1
  1463.  
  1464.     select
  1465.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1466.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1467.         @table_name TABLE_NAME,
  1468.         COLUMN_NAME,
  1469.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1470.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1471.         PRIVILEGE,
  1472.         IS_GRANTABLE
  1473.     from #column_priv2
  1474.     where COLUMN_NAME like @column_name
  1475.     order by 4, 7
  1476. go
  1477.  
  1478. if (charindex('6.50', @@version) = 0 and
  1479.     charindex('7.00', @@version) = 0 and
  1480.     charindex('8.00', @@version) = 0)
  1481. begin
  1482.     print ''
  1483.     print ''
  1484.     print 'Warning:'
  1485.     print 'you are installing the stored procedures '
  1486.     print 'on a pre 6.50 SQL Server.'
  1487.     print 'Ignore the following errors.'
  1488. end
  1489. else
  1490.     drop proc sp_column_privileges
  1491. go
  1492.  
  1493. /*    Procedure for 6.50 server */
  1494. CREATE PROCEDURE sp_column_privileges (
  1495.             @table_name         varchar(32),
  1496.             @table_owner        varchar(32) = null,
  1497.             @table_qualifier    varchar(32) = null,
  1498.             @column_name        varchar(96) = null)    /* 3*32 */
  1499. as
  1500.  
  1501.     declare @table_id     int
  1502.  
  1503.     if @column_name is null /*    If column name not supplied, match all */
  1504.         select @column_name = '%'
  1505.  
  1506.     if @table_qualifier is not null
  1507.     begin
  1508.         if db_name() <> @table_qualifier
  1509.         begin    /* If qualifier doesn't match current database */
  1510.             raiserror (15250, -1,-1)
  1511.             return
  1512.         end
  1513.     end
  1514.     if @table_owner is null
  1515.     begin    /* If unqualified table name */
  1516.         select @table_id = object_id(@table_name)
  1517.     end
  1518.     else
  1519.     begin    /* Qualified table name */
  1520.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1521.     end
  1522.  
  1523.     select
  1524.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1525.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1526.         @table_name TABLE_NAME,
  1527.         convert(varchar(32),c.name) COLUMN_NAME,
  1528.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1529.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1530.         convert (varchar(32),case p.action
  1531.              when 193 then 'SELECT'
  1532.              when 195 then 'INSERT'
  1533.              when 197 then 'UPDATE'
  1534.              else 'REFERENCES'
  1535.         end) PRIVILEGE,
  1536.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1537.             else 'YES'
  1538.         end) IS_GRANTABLE
  1539.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1540.     where
  1541.         c.id = @table_id
  1542.         and c.name like @column_name
  1543.         and c.id = p.id
  1544.         and c.id = o.id
  1545.         and case substring(p.columns, 1, 1) & 1
  1546.                 when NULL then 255    /* all columns have permission */
  1547.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1548.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1549.             end
  1550.             & v.high <> 0            /* permission applies to this column */
  1551.         and v.number <= (select count(*) from syscolumns
  1552.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1553.         and v.type = 'P'
  1554.         and v.number = c.colid
  1555.             /* expand groups */
  1556.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1557.              (p.uid = u.gid and u.uid <> u.gid))
  1558.         and p.protecttype <> 206    /* only grant rows */
  1559.         and p.action in (26,193,195,197)
  1560.         and o.uid <> u.uid            /* no rows for owner */
  1561.         and not exists (            /* exclude revoke'd privileges */
  1562.             select *
  1563.             from sysprotects p1
  1564.             where
  1565.                 p1.protecttype = 206
  1566.                 and p1.action = p.action
  1567.                 and p1.id = p.id
  1568.                 and p1.uid = u.uid
  1569.                 and case substring(p1.columns, 1, 1) & 1
  1570.                         when NULL then 255    /* all columns have permission */
  1571.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1572.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1573.                     end
  1574.                     & v.high <> 0)            /* permission applies to this column */
  1575.     union
  1576.     select    /*    Add rows for table owner */
  1577.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1578.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1579.         @table_name TABLE_NAME,
  1580.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1581.         convert(varchar(32),user_name(u.uid)) grantor,
  1582.         convert(varchar(32),user_name(o.uid)) grantee,
  1583.         convert (varchar(32),case v.number
  1584.             when 193 then 'SELECT'
  1585.             when 195 then 'INSERT'
  1586.             when 197 then 'UPDATE'
  1587.             else 'REFERENCES'
  1588.         end) PRIVILEGE,
  1589.         convert(varchar(3),'YES') IS_GRANTABLE
  1590.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1591.     where
  1592.         c.id = @table_id
  1593.         and c.name like @column_name
  1594.         and c.id = o.id
  1595.         and u.uid = 1        /* grantor is dbo of database */
  1596.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1597.         and v.number in (26,193,195,197)
  1598.         and not exists (    /* exclude revoke'd privileges */
  1599.             select *
  1600.             from sysprotects p1
  1601.             where
  1602.                 p1.protecttype = 206
  1603.                 and p1.action = v.number
  1604.                 and p1.id = o.id
  1605.                 and p1.uid = o.uid)
  1606.     order by 4, 7
  1607. go
  1608.  
  1609.  
  1610.  
  1611. if (charindex('7.00', @@version) = 0 and
  1612.     charindex('8.00', @@version) = 0)
  1613. begin
  1614.     print ''
  1615.     print ''
  1616.     print 'Warning:'
  1617.     print 'you are installing the stored procedures '
  1618.     print 'on a pre 8.0 SQL Server.'
  1619.     print 'Ignore the following errors.'
  1620. end
  1621. else
  1622.     drop proc sp_column_privileges
  1623. go
  1624.  
  1625. /*    Procedure for 8.0 server */
  1626. CREATE PROCEDURE sp_column_privileges (
  1627.             @table_name         sysname,
  1628.             @table_owner        sysname = null,
  1629.             @table_qualifier    sysname = null,
  1630.             @column_name        nvarchar(384) = null)    /* 3*128 */
  1631. as
  1632.  
  1633.     declare @table_id     int
  1634.  
  1635.     if @column_name is null /*    If column name not supplied, match all */
  1636.         select @column_name = '%'
  1637.  
  1638.     if @table_qualifier is not null
  1639.     begin
  1640.         if db_name() <> @table_qualifier
  1641.         begin    /* If qualifier doesn't match current database */
  1642.             raiserror (15250, -1,-1)
  1643.             return
  1644.         end
  1645.     end
  1646.     if @table_owner is null
  1647.     begin    /* If unqualified table name */
  1648.         select @table_id = object_id(quotename(@table_name))
  1649.     end
  1650.     else
  1651.     begin    /* Qualified table name */
  1652.         if @table_owner = N''
  1653.         begin    /* If empty owner name */
  1654.             select @table_id = 0
  1655.         end
  1656.         else
  1657.         begin
  1658.             select @table_id = object_id(quotename(@table_owner) +
  1659.                 '.' + quotename(@table_name))
  1660.         end
  1661.     end
  1662.  
  1663.     select
  1664.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1665.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1666.         @table_name TABLE_NAME,
  1667.         convert(sysname,c.name) COLUMN_NAME,
  1668.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1669.         convert(sysname,user_name(u.uid)) GRANTEE,
  1670.         convert (varchar(32),case p.action
  1671.              when 193 then 'SELECT'
  1672.              when 195 then 'INSERT'
  1673.              when 197 then 'UPDATE'
  1674.              else 'REFERENCES'
  1675.         end) PRIVILEGE,
  1676.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1677.             else 'YES'
  1678.         end) IS_GRANTABLE
  1679.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1680.     where
  1681.         c.id = @table_id
  1682.         and c.name like @column_name
  1683.         and c.id = p.id
  1684.         and c.id = o.id
  1685.         and case substring(p.columns, 1, 1) & 1
  1686.                 when NULL then 255    /* all columns have permission */
  1687.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1688.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1689.             end
  1690.             & v.high <> 0            /* permission applies to this column */
  1691.         and v.number <= (select count(*) from syscolumns
  1692.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1693.         and v.type = N'P'
  1694.         and v.number = c.colid
  1695.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1696.         and (u.uid > 0 and u.uid < 16384)
  1697.         and ((p.uid = u.uid) or 
  1698.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1699.         and p.protecttype <> 206    /* only grant rows */
  1700.         and p.action in (26,193,195,197)
  1701.         and o.uid <> u.uid            /* no rows for owner */
  1702.         and not exists (            /* exclude revoke'd privileges */
  1703.             select *
  1704.             from sysprotects p1
  1705.             where
  1706.                 p1.protecttype = 206
  1707.                 and p1.action = p.action
  1708.                 and p1.id = p.id
  1709.                 and p1.uid = u.uid
  1710.                 and case substring(p1.columns, 1, 1) & 1
  1711.                         when NULL then 255    /* all columns have permission */
  1712.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1713.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1714.                     end
  1715.                     & v.high <> 0)            /* permission applies to this column */
  1716.     union
  1717.     select    /*    Add rows for table owner */
  1718.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1719.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1720.         @table_name TABLE_NAME,
  1721.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1722.         convert(sysname,user_name(u.uid)) grantor,
  1723.         convert(sysname,user_name(o.uid)) grantee,
  1724.         convert (varchar(32),case v.number
  1725.             when 193 then 'SELECT'
  1726.             when 195 then 'INSERT'
  1727.             when 197 then 'UPDATE'
  1728.             else 'REFERENCES'
  1729.         end) PRIVILEGE,
  1730.         convert(varchar(3),'YES') IS_GRANTABLE
  1731.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1732.     where
  1733.         c.id = @table_id
  1734.         and c.name like @column_name
  1735.         and c.id = o.id
  1736.         and u.uid = 1        /* grantor is 'dbo' of database */
  1737.         and v.type = N'P'    /* cross product to get all exposed privileges */
  1738.         and v.number in (26,193,195,197)
  1739.         and not exists (    /* exclude revoke'd privileges */
  1740.             select *
  1741.             from sysprotects p1
  1742.             where
  1743.                 p1.protecttype = 206
  1744.                 and p1.action = v.number
  1745.                 and p1.id = o.id
  1746.                 and p1.uid = o.uid)
  1747.     order by 4, 7
  1748. go
  1749.  
  1750.  
  1751. grant execute on sp_column_privileges to public
  1752. go
  1753.  
  1754. dump tran master with no_log
  1755. go
  1756.  
  1757. print 'creating sp_columns'
  1758. go
  1759.  
  1760. /*    Procedure for pre-6.0 server */
  1761. CREATE PROCEDURE sp_columns (
  1762.                  @table_name        varchar(96),
  1763.                  @table_owner        varchar(96) = null,
  1764.                  @table_qualifier    varchar(32) = null,
  1765.                  @column_name        varchar(96) = null,
  1766.                  @ODBCVer            int = 2)
  1767. AS
  1768.     DECLARE @full_table_name varchar(193)
  1769.     DECLARE @table_id        int
  1770.  
  1771.     if @ODBCVer <> 3
  1772.         select @ODBCVer = 2
  1773.     if @column_name is null /*    If column name not supplied, match all */
  1774.         select @column_name = '%'
  1775.     if @table_qualifier is not null
  1776.     begin
  1777.         if db_name() <> @table_qualifier
  1778.         begin    /* If qualifier doesn't match current database */
  1779.             raiserror 20001 '~~Rush_5~~'
  1780.             return
  1781.         end
  1782.     end
  1783.     if @table_name is null
  1784.     begin    /*    If table name not supplied, match all */
  1785.         select @table_name = '%'
  1786.     end
  1787.     if @table_owner is null
  1788.     begin    /* If unqualified table name */
  1789.         SELECT @full_table_name = @table_name
  1790.     end
  1791.     else
  1792.     begin    /* Qualified table name */
  1793.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1794.     end
  1795.  
  1796.     /*    Get Object ID */
  1797.     SELECT @table_id = object_id(@full_table_name)
  1798.     if ((charindex('%',@full_table_name) = 0) and
  1799.         (charindex('_',@full_table_name) = 0) and
  1800.         @table_id <> 0)
  1801.     begin
  1802.         /* this block is for the case where there is no pattern
  1803.              matching required for the table name */
  1804.  
  1805.         SELECT
  1806.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1807.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1808.             TABLE_NAME = convert(varchar(32),o.name),
  1809.             COLUMN_NAME = convert(varchar(32),c.name),
  1810.             d.DATA_TYPE,
  1811.             TYPE_NAME = t.name,
  1812.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1813.             LENGTH = isnull(d.length, convert(int,c.length)),
  1814.             SCALE = d.numeric_scale,
  1815.             d.RADIX,
  1816.             NULLABLE =    /* set nullability from status flag */
  1817.                 convert(smallint, convert(bit, c.status&8)),
  1818.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1819.             COLUMN_DEF = text,
  1820.             d.SQL_DATA_TYPE,
  1821.             d.SQL_DATETIME_SUB,
  1822.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1823.             ORDINAL_POSITION = convert(int,
  1824.                        (
  1825.                         select count(*)
  1826.                         from syscolumns sc
  1827.                         where sc.id     =  c.id
  1828.                           AND sc.number =  c.number
  1829.                           AND sc.colid  <= c.colid
  1830.                         )),
  1831.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1832.             SS_DATA_TYPE = c.type
  1833.         FROM
  1834.             syscolumns c,
  1835.             sysobjects o,
  1836.             syscomments m,
  1837.             master.dbo.spt_datatype_info d,
  1838.             systypes t
  1839.         WHERE
  1840.             o.id = @table_id
  1841.             AND c.id = o.id
  1842.             AND t.type = d.ss_dtype
  1843.             AND c.length = isnull(d.fixlen, c.length)
  1844.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1845.             AND o.type <> 'P'
  1846.             AND c.usertype = t.usertype
  1847.             AND c.name like @column_name
  1848.             AND c.cdefault *= m.id
  1849.             AND m.colid = 1
  1850.         ORDER BY 17
  1851.     end
  1852.     else
  1853.     begin
  1854.         /* this block is for the case where there IS pattern
  1855.              matching done on the table name */
  1856.         if @table_owner is null /*    If owner not supplied, match all */
  1857.             select @table_owner = '%'
  1858.  
  1859.         SELECT
  1860.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1861.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1862.             TABLE_NAME = convert(varchar(32),o.name),
  1863.             COLUMN_NAME = convert(varchar(32),c.name),
  1864.             d.DATA_TYPE,
  1865.             TYPE_NAME = t.name,
  1866.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1867.             LENGTH = isnull(d.length, convert(int,c.length)),
  1868.             SCALE = d.numeric_scale,
  1869.             d.RADIX,
  1870.             NULLABLE =    /* set nullability from status flag */
  1871.                 convert(smallint, convert(bit, c.status&8)),
  1872.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1873.             COLUMN_DEF = text,
  1874.             d.SQL_DATA_TYPE,
  1875.             d.SQL_DATETIME_SUB,
  1876.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1877.             ORDINAL_POSITION = convert(int,
  1878.                        (
  1879.                         select count(*)
  1880.                         from syscolumns sc
  1881.                         where sc.id     =  c.id
  1882.                           AND sc.number =  c.number
  1883.                           AND sc.colid  <= c.colid
  1884.                         )),
  1885.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1886.             SS_DATA_TYPE = c.type
  1887.         FROM
  1888.             syscolumns c,
  1889.             sysobjects o,
  1890.             syscomments m,
  1891.             master.dbo.spt_datatype_info d,
  1892.             systypes t
  1893.         WHERE
  1894.             o.name like @table_name
  1895.             AND user_name(o.uid) like @table_owner
  1896.             AND o.id = c.id
  1897.             AND t.type = d.ss_dtype
  1898.             AND c.length = isnull(d.fixlen, c.length)
  1899.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1900.             AND o.type <> 'P'
  1901.             AND c.usertype = t.usertype
  1902.             AND c.name like @column_name
  1903.             AND c.cdefault *= m.id
  1904.             AND m.colid = 1
  1905.         ORDER BY 2, 3, 17
  1906.     end
  1907. go
  1908.  
  1909. if (charindex('6.00', @@version) = 0 and
  1910.     charindex('6.50', @@version) = 0 and
  1911.     charindex('7.00', @@version) = 0 and
  1912.     charindex('8.00', @@version) = 0)
  1913. begin
  1914.     print ''
  1915.     print ''
  1916.     print 'Warning:'
  1917.     print 'you are installing the stored procedures '
  1918.     print 'on a pre 6.0 SQL Server.'
  1919.     print 'Ignore the following error.'
  1920. end
  1921. else
  1922.     drop proc sp_columns
  1923. go
  1924.  
  1925. /*    Procedure for 6.0 and 6.50 server */
  1926. CREATE PROCEDURE sp_columns (
  1927.                  @table_name        varchar(96),
  1928.                  @table_owner        varchar(96) = null,
  1929.                  @table_qualifier    varchar(32) = null,
  1930.                  @column_name        varchar(96) = null,
  1931.                  @ODBCVer            int = 2)
  1932. AS
  1933.     DECLARE @full_table_name    varchar(193)
  1934.     DECLARE @table_id int
  1935.  
  1936.     if @ODBCVer <> 3
  1937.         select @ODBCVer = 2
  1938.     if @column_name is null /*    If column name not supplied, match all */
  1939.         select @column_name = '%'
  1940.     if @table_qualifier is not null
  1941.     begin
  1942.         if db_name() <> @table_qualifier
  1943.         begin    /* If qualifier doesn't match current database */
  1944.             raiserror (15250, -1,-1)
  1945.             return
  1946.         end
  1947.     end
  1948.     if @table_name is null
  1949.     begin
  1950.         /* If table name not supplied, match all */
  1951.         select @table_name = '%'
  1952.     end
  1953.     if @table_owner is null
  1954.     begin
  1955.         /* If unqualified table name */
  1956.         SELECT @full_table_name = @table_name
  1957.     end
  1958.     else
  1959.     begin
  1960.         /* Qualified table name */
  1961.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1962.     end
  1963.  
  1964.     /* Get Object ID */
  1965.     SELECT @table_id = object_id(@full_table_name)
  1966.     if ((charindex('%',@full_table_name) = 0) and
  1967.         (charindex('[',@full_table_name) = 0) and
  1968.         (charindex('_',@full_table_name) = 0) and
  1969.         @table_id <> 0)
  1970.     begin
  1971.         /* this block is for the case where there is no pattern
  1972.             matching required for the table name */
  1973.  
  1974.         SELECT
  1975.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1976.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1977.             TABLE_NAME = convert(varchar(32),o.name),
  1978.             COLUMN_NAME = convert(varchar(32),c.name),
  1979.             DATA_TYPE = d.DATA_TYPE,
  1980.             TYPE_NAME = convert(varchar(32),
  1981.                     case
  1982.                         when t.usertype > 100 or t.usertype in (18,80) then t.name
  1983.                         else d.TYPE_NAME
  1984.                     end),
  1985.             "PRECISION" = convert(int,
  1986.                     case
  1987.                         when d.DATA_TYPE in (6,7) then d.data_precision    /* FLOAT/REAL */
  1988.                         else isnull(convert(int,c.prec), 2147483647)
  1989.                     end),
  1990.             LENGTH = convert(int,
  1991.                     case
  1992.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1993.                         c.prec+2
  1994.                         else
  1995.                             isnull(d.length, c.length)
  1996.                     end),
  1997.             SCALE = convert(smallint, c.scale),
  1998.             RADIX = d.RADIX,
  1999.             NULLABLE =    /* set nullability from status flag */
  2000.                 convert(smallint, convert(bit, c.status&8)),
  2001.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2002.             COLUMN_DEF = text,
  2003.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2004.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2005.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  2006.             ORDINAL_POSITION = convert(int,
  2007.                        (
  2008.                         select count(*)
  2009.                         from syscolumns sc
  2010.                         where sc.id     =  c.id
  2011.                           AND sc.number =  c.number
  2012.                           AND sc.colid  <= c.colid
  2013.                         )),
  2014.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2015.             SS_DATA_TYPE = c.type
  2016.         FROM
  2017.             syscolumns c,
  2018.             sysobjects o,
  2019.             syscomments m,
  2020.             master.dbo.spt_datatype_info d,
  2021.             systypes t
  2022.         WHERE
  2023.             o.id = @table_id
  2024.             AND c.id = o.id
  2025.             AND t.type = d.ss_dtype
  2026.             AND c.length = isnull(d.fixlen, c.length)
  2027.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2028.             AND o.type <> 'P'
  2029.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2030.             AND c.usertype = t.usertype
  2031.             AND c.name like @column_name
  2032.             AND c.cdefault *= m.id
  2033.             AND m.colid = 1
  2034.         ORDER BY 17
  2035.     end
  2036.     else
  2037.     begin
  2038.         /* this block is for the case where there IS pattern
  2039.             matching done on the table name */
  2040.  
  2041.         if @table_owner is null /*    If owner not supplied, match all */
  2042.             select @table_owner = '%'
  2043.  
  2044.         SELECT
  2045.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  2046.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  2047.             TABLE_NAME = convert(varchar(32),o.name),
  2048.             COLUMN_NAME = convert(varchar(32),c.name),
  2049.             DATA_TYPE = d.DATA_TYPE,
  2050.             TYPE_NAME = convert(varchar(32), 
  2051.                 case 
  2052.                     when t.usertype > 100 or t.usertype in (18,80) then t.name
  2053.                     else d.TYPE_NAME
  2054.                 end),
  2055.             "PRECISION" = convert(int,
  2056.                     case
  2057.                         when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2058.                         else isnull(convert(int,c.prec), 2147483647)
  2059.                     end),
  2060.             LENGTH = convert(int,
  2061.                     case
  2062.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  2063.                             c.prec+2
  2064.                         else
  2065.                             isnull(d.length, c.length)
  2066.                     end),
  2067.             SCALE = convert(smallint, c.scale),
  2068.             RADIX = d.RADIX,
  2069.             NULLABLE =    /* set nullability from status flag */
  2070.                 convert(smallint, convert(bit, c.status&8)),
  2071.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2072.             COLUMN_DEF = text,
  2073.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2074.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2075.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647) + d.charbin,
  2076.             ORDINAL_POSITION = convert(int,
  2077.                        (
  2078.                         select count(*)
  2079.                         from syscolumns sc
  2080.                         where sc.id     =  c.id
  2081.                           AND sc.number =  c.number
  2082.                           AND sc.colid  <= c.colid
  2083.                         )),
  2084.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2085.             SS_DATA_TYPE = c.type
  2086.         FROM
  2087.             syscolumns c,
  2088.             sysobjects o,
  2089.             syscomments m,
  2090.             master.dbo.spt_datatype_info d,
  2091.             systypes t
  2092.         WHERE
  2093.             o.name like @table_name
  2094.             AND user_name(o.uid) like @table_owner
  2095.             AND o.id = c.id
  2096.             AND t.type = d.ss_dtype
  2097.             AND c.length = isnull(d.fixlen, c.length)
  2098.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2099.             AND o.type <> 'P'
  2100.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2101.             AND c.usertype = t.usertype
  2102.             AND c.name like @column_name
  2103.             AND c.cdefault *= m.id
  2104.             AND m.colid = 1
  2105.         ORDER BY 2, 3, 17
  2106.     end
  2107. go
  2108.  
  2109. if (charindex('7.00', @@version) = 0 and
  2110.     charindex('8.00', @@version) = 0)
  2111. begin
  2112.     print ''
  2113.     print ''
  2114.     print 'Warning:'
  2115.     print 'you are installing the stored procedures '
  2116.     print 'on a pre 7.0 SQL Server.'
  2117.     print 'Ignore the following errors.'
  2118. end
  2119. else
  2120.     drop proc sp_columns
  2121. go
  2122.  
  2123. /*    Procedure for 7.0 server */
  2124. CREATE PROCEDURE sp_columns (
  2125.                  @table_name        nvarchar(384),
  2126.                  @table_owner        nvarchar(384) = null,
  2127.                  @table_qualifier    sysname = null,
  2128.                  @column_name        nvarchar(384) = null,
  2129.                  @ODBCVer            int = 2)
  2130. AS
  2131.     DECLARE @full_table_name    nvarchar(769)
  2132.     DECLARE @table_id int
  2133.  
  2134.     if @ODBCVer <> 3
  2135.         select @ODBCVer = 2
  2136.     if @column_name is null /*    If column name not supplied, match all */
  2137.         select @column_name = '%'
  2138.     if @table_qualifier is not null
  2139.     begin
  2140.         if db_name() <> @table_qualifier
  2141.         begin    /* If qualifier doesn't match current database */
  2142.             raiserror (15250, -1,-1)
  2143.             return
  2144.         end
  2145.     end
  2146.     if @table_name is null
  2147.     begin    /*    If table name not supplied, match all */
  2148.         select @table_name = '%'
  2149.     end
  2150.     if @table_owner is null
  2151.     begin    /* If unqualified table name */
  2152.         SELECT @full_table_name = quotename(@table_name)
  2153.     end
  2154.     else
  2155.     begin    /* Qualified table name */
  2156.         if @table_owner = ''
  2157.         begin    /* If empty owner name */
  2158.             SELECT @full_table_name = quotename(@table_owner)
  2159.         end
  2160.         else
  2161.         begin
  2162.             SELECT @full_table_name = quotename(@table_owner) +
  2163.                 '.' + quotename(@table_name)
  2164.         end
  2165.     end
  2166.  
  2167.     /*    Get Object ID */
  2168.     SELECT @table_id = object_id(@full_table_name)
  2169.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2170.         (isnull(charindex('[', @table_name),0) = 0) and
  2171.         (isnull(charindex('[', @table_owner),0) = 0) and
  2172.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2173.         @table_id <> 0)
  2174.     begin
  2175.         /* this block is for the case where there is no pattern
  2176.             matching required for the table name */
  2177.         
  2178.         SELECT
  2179.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2180.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2181.             TABLE_NAME = convert(sysname,o.name),
  2182.             COLUMN_NAME = convert(sysname,c.name),
  2183.             d.DATA_TYPE,
  2184.             convert (sysname,case
  2185.                 when t.xusertype > 255 then t.name
  2186.                 else d.TYPE_NAME
  2187.             end) TYPE_NAME,
  2188.             convert(int,case
  2189.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2190.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2191.             end) "PRECISION",
  2192.             convert(int,case
  2193.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2194.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2195.                 else
  2196.                     isnull(d.length, c.length)
  2197.             end) LENGTH,
  2198.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2199.             d.RADIX,
  2200.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2201.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2202.             COLUMN_DEF = text,
  2203.             d.SQL_DATA_TYPE,
  2204.             d.SQL_DATETIME_SUB,
  2205.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2206.             ORDINAL_POSITION = convert(int,
  2207.                        (
  2208.                         select count(*)
  2209.                         from syscolumns sc
  2210.                         where sc.id     =  c.id
  2211.                           AND sc.number =  c.number
  2212.                           AND sc.colid  <= c.colid
  2213.                         )),
  2214.             IS_NULLABLE = convert(varchar(254),
  2215.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2216.             SS_DATA_TYPE = c.type
  2217.         FROM
  2218.             sysobjects o,
  2219.             master.dbo.spt_datatype_info d,
  2220.             systypes t,
  2221.             syscolumns c
  2222.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2223.                 AND m.colid = 1
  2224.         WHERE
  2225.             o.id = @table_id
  2226.             AND c.id = o.id
  2227.             AND t.xtype = d.ss_dtype
  2228.             AND c.length = isnull(d.fixlen, c.length)
  2229.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2230.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2231.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2232.             AND c.xusertype = t.xusertype
  2233.             AND c.name like @column_name
  2234.         ORDER BY 17
  2235.     end
  2236.     else
  2237.     begin
  2238.         /* this block is for the case where there IS pattern
  2239.             matching done on the table name */
  2240.  
  2241.         if @table_owner is null /*    If owner not supplied, match all */
  2242.             select @table_owner = '%'
  2243.  
  2244.         SELECT
  2245.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2246.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2247.             TABLE_NAME = convert(sysname,o.name),
  2248.             COLUMN_NAME = convert(sysname,c.name),
  2249.             d.DATA_TYPE,
  2250.             convert (sysname,case
  2251.                 when t.xusertype > 255 then t.name
  2252.                 else d.TYPE_NAME
  2253.             end) TYPE_NAME,
  2254.             convert(int,case
  2255.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2256.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2257.             end) "PRECISION",
  2258.             convert(int,case
  2259.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2260.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2261.                 else
  2262.                     isnull(d.length, c.length)
  2263.             end) LENGTH,
  2264.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2265.             d.RADIX,
  2266.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2267.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2268.             COLUMN_DEF = text,
  2269.             d.SQL_DATA_TYPE,
  2270.             d.SQL_DATETIME_SUB,
  2271.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2272.             ORDINAL_POSITION = convert(int,
  2273.                        (
  2274.                         select count(*)
  2275.                         from syscolumns sc
  2276.                         where sc.id     =  c.id
  2277.                           AND sc.number =  c.number
  2278.                           AND sc.colid  <= c.colid
  2279.                         )),
  2280.             IS_NULLABLE = convert(varchar(254),
  2281.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2282.             SS_DATA_TYPE = c.type
  2283.         FROM
  2284.             sysobjects o,
  2285.             master.dbo.spt_datatype_info d,
  2286.             systypes t,
  2287.             syscolumns c
  2288.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2289.                 AND m.colid = 1
  2290.         WHERE
  2291.             o.name like @table_name
  2292.             AND user_name(o.uid) like @table_owner
  2293.             AND o.id = c.id
  2294.             AND t.xtype = d.ss_dtype
  2295.             AND c.length = isnull(d.fixlen, c.length)
  2296.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2297.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2298.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2299.             AND c.xusertype = t.xusertype
  2300.             AND c.name like @column_name
  2301.         ORDER BY 2, 3, 17
  2302.     end
  2303. go
  2304.  
  2305. if (charindex('8.00', @@version) = 0)
  2306. begin
  2307.     print ''
  2308.     print ''
  2309.     print 'Warning:'
  2310.     print 'you are installing the stored procedures '
  2311.     print 'on a pre 8.0 SQL Server.'
  2312.     print 'Ignore the following errors.'
  2313. end
  2314. else
  2315.     drop proc sp_columns
  2316. go
  2317.  
  2318. /*    Procedure for 8.0 server */
  2319. CREATE PROCEDURE sp_columns (
  2320.                  @table_name        nvarchar(384),
  2321.                  @table_owner        nvarchar(384) = null,
  2322.                  @table_qualifier    sysname = null,
  2323.                  @column_name        nvarchar(384) = null,
  2324.                  @ODBCVer            int = 2)
  2325. AS
  2326.     DECLARE @full_table_name    nvarchar(769)
  2327.     DECLARE @table_id int
  2328.  
  2329.     if @ODBCVer <> 3
  2330.         select @ODBCVer = 2
  2331.     if @column_name is null /*    If column name not supplied, match all */
  2332.         select @column_name = '%'
  2333.     if @table_qualifier is not null
  2334.     begin
  2335.         if db_name() <> @table_qualifier
  2336.         begin    /* If qualifier doesn't match current database */
  2337.             raiserror (15250, -1,-1)
  2338.             return
  2339.         end
  2340.     end
  2341.     if @table_name is null
  2342.     begin    /*    If table name not supplied, match all */
  2343.         select @table_name = '%'
  2344.     end
  2345.     if @table_owner is null
  2346.     begin    /* If unqualified table name */
  2347.         SELECT @full_table_name = quotename(@table_name)
  2348.     end
  2349.     else
  2350.     begin    /* Qualified table name */
  2351.         if @table_owner = ''
  2352.         begin    /* If empty owner name */
  2353.             SELECT @full_table_name = quotename(@table_owner)
  2354.         end
  2355.         else
  2356.         begin
  2357.             SELECT @full_table_name = quotename(@table_owner) +
  2358.                 '.' + quotename(@table_name)
  2359.         end
  2360.     end
  2361.  
  2362.     /*    Get Object ID */
  2363.     SELECT @table_id = object_id(@full_table_name)
  2364.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2365.         (isnull(charindex('[', @table_name),0) = 0) and
  2366.         (isnull(charindex('[', @table_owner),0) = 0) and
  2367.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2368.         @table_id <> 0)
  2369.     begin
  2370.         /* this block is for the case where there is no pattern
  2371.             matching required for the table name */
  2372.         
  2373.         SELECT
  2374.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2375.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2376.             TABLE_NAME = convert(sysname,o.name),
  2377.             COLUMN_NAME = convert(sysname,c.name),
  2378.             d.DATA_TYPE,
  2379.             convert (sysname,case
  2380.                 when t.xusertype > 255 then t.name
  2381.                 else d.TYPE_NAME collate database_default
  2382.             end) TYPE_NAME,
  2383.             convert(int,case
  2384.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2385.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2386.             end) "PRECISION",
  2387.             convert(int,case
  2388.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2389.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2390.                 else
  2391.                     isnull(d.length, c.length)
  2392.             end) LENGTH,
  2393.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2394.             d.RADIX,
  2395.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2396.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2397.             COLUMN_DEF = text,
  2398.             d.SQL_DATA_TYPE,
  2399.             d.SQL_DATETIME_SUB,
  2400.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2401.             ORDINAL_POSITION = convert(int,
  2402.                        (
  2403.                         select count(*)
  2404.                         from syscolumns sc
  2405.                         where sc.id     =  c.id
  2406.                           AND sc.number =  c.number
  2407.                           AND sc.colid  <= c.colid
  2408.                         )),
  2409.             IS_NULLABLE = convert(varchar(254),
  2410.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2411.             SS_DATA_TYPE = c.type
  2412.         FROM
  2413.             sysobjects o,
  2414.             master.dbo.spt_datatype_info d,
  2415.             systypes t,
  2416.             syscolumns c
  2417.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2418.                 AND m.colid = 1
  2419.         WHERE
  2420.             o.id = @table_id
  2421.             AND c.id = o.id
  2422.             AND t.xtype = d.ss_dtype
  2423.             AND c.length = isnull(d.fixlen, c.length)
  2424.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2425.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2426.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2427.             AND c.xusertype = t.xusertype
  2428.             AND c.name like @column_name
  2429.         ORDER BY 17
  2430.     end
  2431.     else
  2432.     begin
  2433.         /* this block is for the case where there IS pattern
  2434.             matching done on the table name */
  2435.  
  2436.         if @table_owner is null /*    If owner not supplied, match all */
  2437.             select @table_owner = '%'
  2438.  
  2439.         SELECT
  2440.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2441.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2442.             TABLE_NAME = convert(sysname,o.name),
  2443.             COLUMN_NAME = convert(sysname,c.name),
  2444.             d.DATA_TYPE,
  2445.             convert (sysname,case
  2446.                 when t.xusertype > 255 then t.name
  2447.                 else d.TYPE_NAME collate database_default
  2448.             end) TYPE_NAME,
  2449.             convert(int,case
  2450.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2451.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2452.             end) "PRECISION",
  2453.             convert(int,case
  2454.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2455.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2456.                 else
  2457.                     isnull(d.length, c.length)
  2458.             end) LENGTH,
  2459.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2460.             d.RADIX,
  2461.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2462.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2463.             COLUMN_DEF = text,
  2464.             d.SQL_DATA_TYPE,
  2465.             d.SQL_DATETIME_SUB,
  2466.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2467.             ORDINAL_POSITION = convert(int,
  2468.                        (
  2469.                         select count(*)
  2470.                         from syscolumns sc
  2471.                         where sc.id     =  c.id
  2472.                           AND sc.number =  c.number
  2473.                           AND sc.colid  <= c.colid
  2474.                         )),
  2475.             IS_NULLABLE = convert(varchar(254),
  2476.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2477.             SS_DATA_TYPE = c.type
  2478.         FROM
  2479.             sysobjects o,
  2480.             master.dbo.spt_datatype_info d,
  2481.             systypes t,
  2482.             syscolumns c
  2483.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2484.                 AND m.colid = 1
  2485.         WHERE
  2486.             o.name like @table_name
  2487.             AND user_name(o.uid) like @table_owner
  2488.             AND o.id = c.id
  2489.             AND t.xtype = d.ss_dtype
  2490.             AND c.length = isnull(d.fixlen, c.length)
  2491.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2492.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2493.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2494.             AND c.xusertype = t.xusertype
  2495.             AND c.name like @column_name
  2496.         ORDER BY 2, 3, 17
  2497.     end
  2498. go
  2499.  
  2500. grant execute on sp_columns to public
  2501. go
  2502.  
  2503. dump tran master with no_log
  2504. go
  2505.  
  2506. print 'creating sp_databases'
  2507. go
  2508.  
  2509. /*    Procedure for pre-7.0 server */
  2510. create proc sp_databases
  2511. as
  2512.     set nocount on
  2513.     /* Use temporary table to sum up database size w/o using group by */
  2514.     create table #databases (
  2515.                   DATABASE_NAME varchar(32) NOT NULL,
  2516.                   size int NOT NULL)
  2517.  
  2518.     /* Insert row for each database */
  2519.     insert into #databases
  2520.         select
  2521.             name,
  2522.             (select sum(size) from master.dbo.sysusages
  2523.                 where dbid = d.dbid)
  2524.         from master.dbo.sysdatabases d
  2525.  
  2526.     select
  2527.          DATABASE_NAME,
  2528.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2529.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2530.     from #databases
  2531.     order by 1
  2532. go
  2533.  
  2534. if (charindex('7.00', @@version) = 0 and
  2535.     charindex('8.00', @@version) = 0)
  2536. begin
  2537.     print ''
  2538.     print ''
  2539.     print 'Warning:'
  2540.     print 'you are installing the stored procedures '
  2541.     print 'on a pre 8.0 SQL Server.'
  2542.     print 'Ignore the following errors.'
  2543. end
  2544. else
  2545.     drop proc sp_databases
  2546. go
  2547.  
  2548. /*    Procedure for 8.0 server */
  2549. create proc sp_databases
  2550. as
  2551.     set nocount on
  2552.     declare @name sysname
  2553.     declare @SQL  nvarchar(600)
  2554.  
  2555.     /* Use temporary table to sum up database size w/o using group by */
  2556.     create table #databases (
  2557.                   DATABASE_NAME sysname NOT NULL,
  2558.                   size int NOT NULL)
  2559.  
  2560.     declare c1 cursor for 
  2561.         select name from master.dbo.sysdatabases
  2562.             where has_dbaccess(name) = 1 -- Only look at databases to which we have access
  2563.  
  2564.     open c1
  2565.     fetch c1 into @name
  2566.  
  2567.     while @@fetch_status >= 0
  2568.     begin
  2569.         select @SQL = 'insert into #databases
  2570.                 select N'''+ @name + ''', sum(size) from '
  2571.                 + QuoteName(@name) + '.dbo.sysfiles'
  2572.         /* Insert row for each database */
  2573.         execute (@SQL)
  2574.         fetch c1 into @name
  2575.     end
  2576.     deallocate c1
  2577.  
  2578.     select    
  2579.         DATABASE_NAME,
  2580.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2581.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2582.     from #databases
  2583.     order by 1
  2584. go
  2585.  
  2586. grant execute on sp_databases to public
  2587. go
  2588.  
  2589. dump tran master with no_log
  2590. go
  2591.  
  2592. print 'creating sp_datatype_info'
  2593. go
  2594.  
  2595. /*    Procedure for pre-6.0 server */
  2596. create proc sp_datatype_info
  2597.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2598. as
  2599.     if @ODBCVer <> 3
  2600.         select @ODBCVer = 2
  2601.     if @data_type = 0
  2602.         select
  2603.             TYPE_NAME = t.name,
  2604.             d.DATA_TYPE,
  2605.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2606.             d.LITERAL_PREFIX,
  2607.             d.LITERAL_SUFFIX,
  2608.             e.CREATE_PARAMS,
  2609.             d.NULLABLE,
  2610.             d.CASE_SENSITIVE,
  2611.             d.SEARCHABLE,
  2612.             d.UNSIGNED_ATTRIBUTE,
  2613.             d.MONEY,
  2614.             d.AUTO_INCREMENT,
  2615.             LOCAL_TYPE_NAME = t.name,
  2616.             MINIMUM_SCALE = d.numeric_scale,
  2617.             MAXIMUM_SCALE = d.numeric_scale,
  2618.             d.SQL_DATA_TYPE,
  2619.             d.SQL_DATETIME_SUB,
  2620.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2621.             INTERVAL_PRECISION = convert(smallint,NULL),
  2622.             USERTYPE = t.usertype
  2623.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2624.         where
  2625.             d.ss_dtype = t.type
  2626.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2627.             and t.usertype *= e.user_type
  2628.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2629.                 t.usertype > 100)
  2630.         order by 2, 12, 11, t.usertype
  2631.  
  2632.     else
  2633.         select
  2634.             TYPE_NAME = t.name,
  2635.             d.DATA_TYPE,
  2636.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2637.             d.LITERAL_PREFIX,
  2638.             d.LITERAL_SUFFIX,
  2639.             e.CREATE_PARAMS,
  2640.             d.NULLABLE,
  2641.             d.CASE_SENSITIVE,
  2642.             d.SEARCHABLE,
  2643.             d.UNSIGNED_ATTRIBUTE,
  2644.             d.MONEY,
  2645.             d.AUTO_INCREMENT,
  2646.             LOCAL_TYPE_NAME = t.name,
  2647.             MINIMUM_SCALE = d.numeric_scale,
  2648.             MAXIMUM_SCALE = d.numeric_scale,
  2649.             d.SQL_DATA_TYPE,
  2650.             d.SQL_DATETIME_SUB,
  2651.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2652.             INTERVAL_PRECISION = convert(smallint,NULL),
  2653.             USERTYPE = t.usertype
  2654.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2655.         where
  2656.             DATA_TYPE = @data_type
  2657.             and d.ss_dtype = t.type
  2658.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2659.             and t.usertype *= e.user_type
  2660.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2661.                 t.usertype > 100)
  2662.         order by 12, 11, t.usertype
  2663.  
  2664. go
  2665.  
  2666. if (charindex('6.00', @@version) = 0 and
  2667.     charindex('6.50', @@version) = 0 and
  2668.     charindex('7.00', @@version) = 0 and
  2669.     charindex('8.00', @@version) = 0)
  2670. begin
  2671.     print ''
  2672.     print ''
  2673.     print 'Warning:'
  2674.     print 'you are installing the stored procedures '
  2675.     print 'on a pre 6.0 SQL Server.'
  2676.     print 'Ignore the following errors.'
  2677. end
  2678. else
  2679.     drop proc sp_datatype_info
  2680. go
  2681.  
  2682. /*    Procedure for 6.0 and 6.50 servers */
  2683. create proc sp_datatype_info
  2684.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2685. as
  2686.     if @ODBCVer <> 3
  2687.         select @ODBCVer = 2
  2688.     if @data_type = 0
  2689.         select
  2690.             convert(varchar(32),case
  2691.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2692.                 else d.TYPE_NAME
  2693.             end) TYPE_NAME,
  2694.             d.DATA_TYPE,
  2695.             convert(int,case
  2696.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2697.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2698.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2699.                 else t.prec
  2700.             end) "PRECISION",
  2701.             d.LITERAL_PREFIX,
  2702.             d.LITERAL_SUFFIX,
  2703.             e.CREATE_PARAMS,
  2704.             convert(smallint,case
  2705.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2706.                 else t.allownulls
  2707.             end) NULLABLE,
  2708.             d.CASE_SENSITIVE,
  2709.             d.SEARCHABLE,
  2710.             d.UNSIGNED_ATTRIBUTE,
  2711.             d.MONEY,
  2712.             d.AUTO_INCREMENT,
  2713.             convert(varchar(32),case
  2714.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2715.                 else d.TYPE_NAME
  2716.             end) LOCAL_TYPE_NAME,
  2717.             convert(smallint,case
  2718.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2719.                 else d.numeric_scale
  2720.             end) MINIMUM_SCALE,
  2721.             convert(smallint,case
  2722.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2723.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2724.                 else t.scale
  2725.             end) MAXIMUM_SCALE,
  2726.             d.SQL_DATA_TYPE,
  2727.             d.SQL_DATETIME_SUB,
  2728.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2729.             INTERVAL_PRECISION = convert(smallint,NULL),
  2730.             USERTYPE = t.usertype
  2731.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2732.         where
  2733.             d.ss_dtype = t.type
  2734.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2735.             and t.usertype *= e.user_type
  2736.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2737.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2738.                 t.usertype > 100)
  2739.             and (t.usertype <= 100 or
  2740.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2741.         order by 2, 12, 11, t.usertype
  2742.  
  2743.     else
  2744.         select
  2745.             convert(varchar(32),case
  2746.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2747.                 else d.TYPE_NAME
  2748.             end) TYPE_NAME,
  2749.             d.DATA_TYPE,
  2750.             convert(int,case
  2751.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2752.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2753.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2754.                 else t.prec
  2755.             end) "PRECISION",
  2756.             d.LITERAL_PREFIX,
  2757.             d.LITERAL_SUFFIX,
  2758.             e.CREATE_PARAMS,
  2759.             convert(smallint,case
  2760.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2761.                 else t.allownulls
  2762.             end) NULLABLE,
  2763.             d.CASE_SENSITIVE,
  2764.             d.SEARCHABLE,
  2765.             d.UNSIGNED_ATTRIBUTE,
  2766.             d.MONEY,
  2767.             d.AUTO_INCREMENT,
  2768.             convert(varchar(32),case
  2769.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2770.                 else d.TYPE_NAME
  2771.             end) LOCAL_TYPE_NAME,
  2772.             convert(smallint,case
  2773.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2774.                 else d.numeric_scale
  2775.             end) MINIMUM_SCALE,
  2776.             convert(smallint,case
  2777.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2778.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2779.                 else t.scale
  2780.             end) MAXIMUM_SCALE,
  2781.             d.SQL_DATA_TYPE,
  2782.             d.SQL_DATETIME_SUB,
  2783.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2784.             INTERVAL_PRECISION = convert(smallint,NULL),
  2785.             USERTYPE = t.usertype
  2786.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2787.         where
  2788.             d.DATA_TYPE = @data_type
  2789.             and d.ss_dtype = t.type
  2790.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2791.             and t.usertype *= e.user_type
  2792.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2793.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2794.                 t.usertype > 100)
  2795.             and (t.usertype <= 100 or
  2796.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2797.         order by 12, 11, t.usertype
  2798. go
  2799.  
  2800. if (charindex('7.00', @@version) = 0 and
  2801.     charindex('8.00', @@version) = 0)
  2802. begin
  2803.     print ''
  2804.     print ''
  2805.     print 'Warning:'
  2806.     print 'you are installing the stored procedures '
  2807.     print 'on a pre 7.0 SQL Server.'
  2808.     print 'Ignore the following errors.'
  2809. end
  2810. else
  2811.     drop proc sp_datatype_info
  2812. go
  2813.  
  2814. /*    Procedure for 7.0 server */
  2815. create proc sp_datatype_info
  2816.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2817. as
  2818.     declare @mintype int
  2819.     declare @maxtype int
  2820.  
  2821.     if @ODBCVer <> 3
  2822.         select @ODBCVer = 2
  2823.     if @data_type = 0
  2824.     begin
  2825.         select @mintype = -32768
  2826.         select @maxtype = 32767
  2827.     end
  2828.     else
  2829.     begin
  2830.         select @mintype = @data_type
  2831.         select @maxtype = @data_type
  2832.     end
  2833.  
  2834.     select
  2835.         convert(sysname,case
  2836.             when t.xusertype > 255 then t.name
  2837.             else d.TYPE_NAME
  2838.         end) TYPE_NAME,
  2839.         d.DATA_TYPE,
  2840.         convert(int,case
  2841.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2842.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  2843.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2844.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2845.         end) "PRECISION",
  2846.         d.LITERAL_PREFIX,
  2847.         d.LITERAL_SUFFIX,
  2848.         e.CREATE_PARAMS,
  2849.         convert(smallint,case
  2850.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2851.             else TypeProperty (t.name, 'AllowsNull')
  2852.         end) NULLABLE,
  2853.         d.CASE_SENSITIVE,
  2854.         d.SEARCHABLE,
  2855.         d.UNSIGNED_ATTRIBUTE,
  2856.         d.MONEY,
  2857.         d.AUTO_INCREMENT,
  2858.         convert(sysname,case
  2859.             when t.xusertype > 255 then t.name
  2860.             else d.LOCAL_TYPE_NAME
  2861.         end) LOCAL_TYPE_NAME,
  2862.         MINIMUM_SCALE = d.numeric_scale,
  2863.         convert(smallint,case
  2864.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2865.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2866.             else TypeProperty (t.name, 'Scale')
  2867.         end) MAXIMUM_SCALE,
  2868.         d.SQL_DATA_TYPE,
  2869.         d.SQL_DATETIME_SUB,
  2870.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2871.         INTERVAL_PRECISION = convert(smallint,NULL),
  2872.         USERTYPE = t.usertype
  2873.     from master.dbo.spt_datatype_info d
  2874.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2875.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2876.             t.xusertype = e.user_type
  2877.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2878.     where
  2879.         d.DATA_TYPE between @mintype and @maxtype
  2880.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2881.         and (t.xusertype <= 255 or
  2882.             isnull(d.AUTO_INCREMENT,0) = 0)
  2883.     order by 2, 12, 11,
  2884.     case
  2885.         when t.usertype=18 then 255
  2886.         else t.usertype
  2887.     end
  2888.  
  2889. go
  2890.  
  2891. if (charindex('8.00', @@version) = 0)
  2892. begin
  2893.     print ''
  2894.     print ''
  2895.     print 'Warning:'
  2896.     print 'you are installing the stored procedures '
  2897.     print 'on a pre 8.0 SQL Server.'
  2898.     print 'Ignore the following errors.'
  2899. end
  2900. else
  2901.     drop proc sp_datatype_info
  2902. go
  2903.  
  2904. /*    Procedure for 8.0 server */
  2905. create proc sp_datatype_info
  2906.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2907. as
  2908.     declare @mintype int
  2909.     declare @maxtype int
  2910.  
  2911.     if @ODBCVer <> 3
  2912.         select @ODBCVer = 2
  2913.     if @data_type = 0
  2914.     begin
  2915.         select @mintype = -32768
  2916.         select @maxtype = 32767
  2917.     end
  2918.     else
  2919.     begin
  2920.         select @mintype = @data_type
  2921.         select @maxtype = @data_type
  2922.     end
  2923.  
  2924.     select
  2925.         convert(sysname,case
  2926.             when t.xusertype > 255 then t.name
  2927.             else d.TYPE_NAME collate database_default
  2928.         end) TYPE_NAME,
  2929.         d.DATA_TYPE,
  2930.         convert(int,case
  2931.             when d.DATA_TYPE in (6,7,-150) then d.data_precision         /* FLOAT/REAL/sql_variant*/
  2932.             when d.ss_dtype in (55,63,106,108) and
  2933.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2934.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2935.         end) "PRECISION",
  2936.         d.LITERAL_PREFIX,
  2937.         d.LITERAL_SUFFIX,
  2938.         e.CREATE_PARAMS,
  2939.         convert(smallint,case
  2940.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2941.             else TypeProperty (t.name, 'AllowsNull')
  2942.         end) NULLABLE,
  2943.         d.CASE_SENSITIVE,
  2944.         d.SEARCHABLE,
  2945.         d.UNSIGNED_ATTRIBUTE,
  2946.         d.MONEY,
  2947.         d.AUTO_INCREMENT,
  2948.         convert(sysname,case
  2949.             when t.xusertype > 255 then t.name
  2950.             else d.LOCAL_TYPE_NAME collate database_default
  2951.         end) LOCAL_TYPE_NAME,
  2952.         convert(smallint,case
  2953.             when d.ss_dtype in (55,63,106,108) and t.xusertype > 255 then TypeProperty (t.name, 'Scale')
  2954.             else d.numeric_scale
  2955.         end) MINIMUM_SCALE,
  2956.         convert(smallint,case
  2957.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2958.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2959.             else TypeProperty (t.name, 'Scale')
  2960.         end) MAXIMUM_SCALE,
  2961.         d.SQL_DATA_TYPE,
  2962.         d.SQL_DATETIME_SUB,
  2963.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2964.         INTERVAL_PRECISION = convert(smallint,NULL),
  2965.         USERTYPE = t.usertype
  2966.     from master.dbo.spt_datatype_info d
  2967.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2968.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2969.             t.xusertype = e.user_type
  2970.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2971.     where
  2972.         d.DATA_TYPE between @mintype and @maxtype
  2973.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2974.         and (t.xusertype <= 255 or
  2975.             isnull(d.AUTO_INCREMENT,0) = 0)
  2976.     order by 2, 12, 11,
  2977.     case
  2978.         when t.usertype=18 then 255
  2979.         else t.usertype
  2980.     end
  2981.  
  2982. go
  2983.  
  2984. grant execute on sp_datatype_info to public
  2985. go
  2986.  
  2987. dump tran master with no_log
  2988. go
  2989.  
  2990. print 'creating sp_fkeys'
  2991. go
  2992.  
  2993. /*    Procedure for pre-6.0 server */
  2994. CREATE PROCEDURE sp_fkeys(
  2995.                @pktable_name        varchar(32) = null,
  2996.                @pktable_owner        varchar(32) = null,
  2997.                @pktable_qualifier    varchar(32) = null,
  2998.                @fktable_name        varchar(32) = null,
  2999.                @fktable_owner        varchar(32) = null,
  3000.                @fktable_qualifier    varchar(32) = null )
  3001. as
  3002.     set nocount on
  3003.     declare    @order_by_pk int
  3004.  
  3005.     select  @order_by_pk = 0
  3006.  
  3007.     if (@pktable_name is null) and (@fktable_name is null)
  3008.     begin    /* If neither primary key nor foreign key table names given */
  3009.         raiserror 20004 '~~Rush_25~~'
  3010.         return
  3011.     end
  3012.     if @fktable_qualifier is not null
  3013.     begin
  3014.         if db_name() <> @fktable_qualifier
  3015.         begin    /* If qualifier doesn't match current database */
  3016.             raiserror 20001 '~~Rush_26~~'
  3017.             return
  3018.         end
  3019.     end
  3020.     if @pktable_qualifier is not null
  3021.     begin
  3022.         if db_name() <> @pktable_qualifier
  3023.         begin    /* If qualifier doesn't match current database */
  3024.             raiserror 20001 '~~Rush_27~~'
  3025.             return
  3026.         end
  3027.     end
  3028.  
  3029.     if @pktable_name is null
  3030.     begin /*  If table name not supplied, match all */
  3031.         select @pktable_name = '%'
  3032.         select @order_by_pk = 1
  3033.     end
  3034.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  3035.         select @pktable_owner = '%'
  3036.     if @fktable_name is null    /*    If table name not supplied, match all */
  3037.         select @fktable_name = '%'
  3038.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  3039.         select @fktable_owner = '%'
  3040.  
  3041.     if @@trancount <> 0
  3042.     begin    /* If inside a transaction */
  3043.         raiserror 20003 '~~Rush_28~~'
  3044.         return
  3045.     end
  3046.     create table #fkeys(
  3047.              PKTABLE_QUALIFIER    varchar(32) NULL,
  3048.              PKTABLE_OWNER        varchar(32) NULL,
  3049.              PKTABLE_NAME        varchar(32) NOT NULL,
  3050.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  3051.              FKTABLE_QUALIFIER    varchar(32) NULL,
  3052.              FKTABLE_OWNER        varchar(32) NULL,
  3053.              FKTABLE_NAME        varchar(32) NOT NULL,
  3054.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  3055.              KEY_SEQ            smallint NOT NULL)
  3056.  
  3057.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  3058.     /*    Process syskeys for each relationship */
  3059.     /*    The inserts below adds a row to the temp table for each of the
  3060.         8 possible relationships */
  3061.     insert into #fkeys
  3062.         select
  3063.             db_name(),
  3064.             user_name(o1.uid),
  3065.             object_name(k.depid),
  3066.             c2.name,
  3067.             db_name(),
  3068.             user_name(o2.uid),
  3069.             object_name(k.id),
  3070.             c1.name,
  3071.             1
  3072.         from
  3073.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3074.         where
  3075.             c1.id = k.id
  3076.             and k.type = 2    /* Foreign type key */
  3077.             and c1.colid = k.key1
  3078.             and c2.id = k.depid
  3079.             and c2.colid = k.depkey1
  3080.             and o1.id = k.depid
  3081.             and o2.id = k.id
  3082.     union all
  3083.         select
  3084.             db_name(),
  3085.             user_name(o1.uid),
  3086.             object_name(k.depid),
  3087.             c2.name,
  3088.             db_name(),
  3089.             user_name(o2.uid),
  3090.             object_name(k.id),
  3091.             c1.name,
  3092.             2
  3093.         from
  3094.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3095.         where
  3096.             c1.id = k.id
  3097.             and k.type = 2    /* Foreign type key */
  3098.             and c1.colid = k.key2
  3099.             and c2.id = k.depid
  3100.             and c2.colid = k.depkey2
  3101.             and o1.id = k.depid
  3102.             and o2.id = k.id
  3103.     union all
  3104.         select
  3105.             db_name(),
  3106.             user_name(o1.uid),
  3107.             object_name(k.depid),
  3108.             c2.name,
  3109.             db_name(),
  3110.             user_name(o2.uid),
  3111.             object_name(k.id),
  3112.             c1.name,
  3113.             3
  3114.         from
  3115.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3116.         where
  3117.             c1.id = k.id
  3118.             and k.type = 2    /* Foreign type key */
  3119.             and c1.colid = k.key3
  3120.             and c2.id = k.depid
  3121.             and c2.colid = k.depkey3
  3122.             and o1.id = k.depid
  3123.             and o2.id = k.id
  3124.     union all
  3125.         select
  3126.             db_name(),
  3127.             user_name(o1.uid),
  3128.             object_name(k.depid),
  3129.             c2.name,
  3130.             db_name(),
  3131.             user_name(o2.uid),
  3132.             object_name(k.id),
  3133.             c1.name,
  3134.             4
  3135.         from
  3136.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3137.         where
  3138.             c1.id = k.id
  3139.             and k.type = 2    /* Foreign type key */
  3140.             and c1.colid = k.key4
  3141.             and c2.id = k.depid
  3142.             and c2.colid = k.depkey4
  3143.             and o1.id = k.depid
  3144.             and o2.id = k.id
  3145.     union all
  3146.         select
  3147.             db_name(),
  3148.             user_name(o1.uid),
  3149.             object_name(k.depid),
  3150.             c2.name,
  3151.             db_name(),
  3152.             user_name(o2.uid),
  3153.             object_name(k.id),
  3154.             c1.name,
  3155.             5
  3156.         from
  3157.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3158.         where
  3159.             c1.id = k.id
  3160.             and k.type = 2    /* Foreign type key */
  3161.             and c1.colid = k.key5
  3162.             and c2.id = k.depid
  3163.             and c2.colid = k.depkey5
  3164.             and o1.id = k.depid
  3165.             and o2.id = k.id
  3166.     union all
  3167.         select
  3168.             db_name(),
  3169.             user_name(o1.uid),
  3170.             object_name(k.depid),
  3171.             c2.name,
  3172.             db_name(),
  3173.             user_name(o2.uid),
  3174.             object_name(k.id),
  3175.             c1.name,
  3176.             6
  3177.         from
  3178.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3179.         where
  3180.             c1.id = k.id
  3181.             and k.type = 2    /* Foreign type key */
  3182.             and c1.colid = k.key6
  3183.             and c2.id = k.depid
  3184.             and c2.colid = k.depkey6
  3185.             and o1.id = k.depid
  3186.             and o2.id = k.id
  3187.     union all
  3188.         select
  3189.             db_name(),
  3190.             user_name(o1.uid),
  3191.             object_name(k.depid),
  3192.             c2.name,
  3193.             db_name(),
  3194.             user_name(o2.uid),
  3195.             object_name(k.id),
  3196.             c1.name,
  3197.             7
  3198.         from
  3199.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3200.         where
  3201.             c1.id = k.id
  3202.             and k.type = 2    /* Foreign type key */
  3203.             and c1.colid = k.key7
  3204.             and c2.id = k.depid
  3205.             and c2.colid = k.depkey7
  3206.             and o1.id = k.depid
  3207.             and o2.id = k.id
  3208.     union all
  3209.         select
  3210.             db_name(),
  3211.             user_name(o1.uid),
  3212.             object_name(k.depid),
  3213.             c2.name,
  3214.             db_name(),
  3215.             user_name(o2.uid),
  3216.             object_name(k.id),
  3217.             c1.name,
  3218.             8
  3219.         from
  3220.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3221.         where
  3222.             c1.id = k.id
  3223.             and k.type = 2    /* Foreign type key */
  3224.             and c1.colid = k.key8
  3225.             and c2.id = k.depid
  3226.             and c2.colid = k.depkey8
  3227.             and o1.id = k.depid
  3228.             and o2.id = k.id
  3229.  
  3230.     if @order_by_pk = 1 /*    If order by PK fields */
  3231.         select
  3232.             PKTABLE_QUALIFIER,
  3233.             PKTABLE_OWNER,
  3234.             PKTABLE_NAME,
  3235.             PKCOLUMN_NAME,
  3236.             FKTABLE_QUALIFIER,
  3237.             FKTABLE_OWNER,
  3238.             FKTABLE_NAME,
  3239.             FKCOLUMN_NAME,
  3240.             KEY_SEQ,
  3241.             UPDATE_RULE = convert(smallint, null),
  3242.             DELETE_RULE = convert(smallint,null),
  3243.             FK_NAME = convert(varchar(32),null),
  3244.             PK_NAME = convert(varchar(32),null),
  3245.             DEFERRABILITY = convert(smallint,7)
  3246.         from #fkeys
  3247.         where FKTABLE_NAME like @fktable_name
  3248.             and FKTABLE_OWNER like @fktable_owner
  3249.             and PKTABLE_NAME  like @pktable_name
  3250.             and PKTABLE_OWNER like @pktable_owner
  3251.         order by 1, 2, 3, 9, 4
  3252.     else        /*    Order by FK fields */
  3253.         select
  3254.             PKTABLE_QUALIFIER,
  3255.             PKTABLE_OWNER,
  3256.             PKTABLE_NAME,
  3257.             PKCOLUMN_NAME,
  3258.             FKTABLE_QUALIFIER,
  3259.             FKTABLE_OWNER,
  3260.             FKTABLE_NAME,
  3261.             FKCOLUMN_NAME,
  3262.             KEY_SEQ,
  3263.             UPDATE_RULE = convert(smallint,null),
  3264.             DELETE_RULE = convert(smallint,null),
  3265.             FK_NAME = convert(varchar(32),null),
  3266.             PK_NAME = convert(varchar(32),null),
  3267.             DEFERRABILITY = convert(smallint,7)
  3268.         from #fkeys
  3269.         where FKTABLE_NAME like @fktable_name
  3270.             and FKTABLE_OWNER like @fktable_owner
  3271.             and PKTABLE_NAME  like @pktable_name
  3272.             and PKTABLE_OWNER like @pktable_owner
  3273.         order by 5, 6, 7, 9, 8
  3274. go
  3275.  
  3276. if (charindex('6.00', @@version) = 0 and
  3277.     charindex('6.50', @@version) = 0 and
  3278.     charindex('7.00', @@version) = 0 and
  3279.     charindex('8.00', @@version) = 0)
  3280. begin
  3281.     print ''
  3282.     print ''
  3283.     print 'Warning:'
  3284.     print 'you are installing the stored procedures '
  3285.     print 'on a pre 6.0 SQL Server.'
  3286.     print 'Ignore the following errors.'
  3287. end
  3288. else
  3289.     drop proc sp_fkeys
  3290. go
  3291.  
  3292. /*    Procedure for 6.0 and 6.50 servers */
  3293. CREATE PROCEDURE sp_fkeys(
  3294.                @pktable_name        varchar(32) = null,
  3295.                @pktable_owner        varchar(32) = null,
  3296.                @pktable_qualifier    varchar(32) = null,
  3297.                @fktable_name        varchar(32) = null,
  3298.                @fktable_owner        varchar(32) = null,
  3299.                @fktable_qualifier    varchar(32) = null )
  3300. as
  3301.     set nocount on
  3302.     DECLARE @pktable_id            int
  3303.     DECLARE @pkfull_table_name    varchar(65) /* 2*32 +1 */
  3304.     DECLARE @fktable_id            int
  3305.     DECLARE @fkfull_table_name    varchar(65) /* 2*32 +1 */
  3306.     declare    @order_by_pk        int
  3307.  
  3308.     select  @order_by_pk = 0
  3309.  
  3310.     if (@pktable_name is null) and (@fktable_name is null)
  3311.     begin    /* If neither primary key nor foreign key table names given */
  3312.         raiserror (15252,-1,-1)
  3313.         return
  3314.     end
  3315.     if @fktable_qualifier is not null
  3316.     begin
  3317.         if db_name() <> @fktable_qualifier
  3318.         begin    /* If qualifier doesn't match current database */
  3319.             raiserror (15250, -1,-1)
  3320.             return
  3321.         end
  3322.     end
  3323.     if @pktable_qualifier is not null
  3324.     begin
  3325.         if db_name() <> @pktable_qualifier
  3326.         begin    /* If qualifier doesn't match current database */
  3327.             raiserror (15250, -1,-1)
  3328.             return
  3329.         end
  3330.     end
  3331.  
  3332.     if @pktable_owner is null
  3333.     begin    /* If unqualified primary key table name */
  3334.         SELECT @pkfull_table_name = @pktable_name
  3335.     end
  3336.     else
  3337.     begin    /* Qualified primary key table name */
  3338.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  3339.     end
  3340.     /*    Get Object ID */
  3341.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3342.  
  3343.     if @fktable_owner is null
  3344.     begin    /* If unqualified foreign key table name */
  3345.         SELECT @fkfull_table_name = @fktable_name
  3346.     end
  3347.     else
  3348.     begin    /* Qualified foreign key table name */
  3349.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  3350.     end
  3351.     /*    Get Object ID */
  3352.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3353.  
  3354.     if @fktable_name is not null
  3355.     begin
  3356.         if @fktable_id is null
  3357.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3358.     end
  3359.  
  3360.     if @pktable_name is null
  3361.     begin /*  If table name not supplied, match all */
  3362.         select @order_by_pk = 1
  3363.     end
  3364.     else
  3365.     begin
  3366.         if @pktable_id is null
  3367.         begin
  3368.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3369.         end
  3370.     end
  3371.  
  3372.     if (@@trancount <> 0 and
  3373.         charindex('6.50', @@version) = 0)
  3374.     begin    /* If inside a transaction */
  3375.         raiserror (15002,-1,-1,'sp_fkeys')
  3376.         return
  3377.     end
  3378.  
  3379.     create table #fkeys(
  3380.              pkdb_id        int NOT NULL,
  3381.              pktable_id     int NOT NULL,
  3382.              pkcolid        int NOT NULL,
  3383.              fkdb_id        int NOT NULL,
  3384.              fktable_id        int NOT NULL,
  3385.              fkcolid        int NOT NULL,
  3386.              KEY_SEQ        smallint NOT NULL,
  3387.              fk_id            int NOT NULL,
  3388.              pk_id            int NOT NULL)
  3389.  
  3390.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3391.     /*    Process syskeys for each relationship */
  3392.     /*    The inserts below adds a row to the temp table for each of the
  3393.         16 possible relationships */
  3394.     insert into #fkeys
  3395.         select
  3396.             r.rkeydbid,
  3397.             r.rkeyid,
  3398.             r.rkey1,
  3399.             r.fkeydbid,
  3400.             r.fkeyid,
  3401.             r.fkey1,
  3402.             1,
  3403.             r.constid,
  3404.             s.constid
  3405.         from
  3406.             sysreferences r, sysconstraints s
  3407.         where    r.rkeyid = s.id
  3408.             AND (s.status & 0xf) = 1
  3409.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3410.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3411.       union all
  3412.         select
  3413.             r.rkeydbid,
  3414.             r.rkeyid,
  3415.             r.rkey2,
  3416.             r.fkeydbid,
  3417.             r.fkeyid,
  3418.             r.fkey2,
  3419.             2,
  3420.             r.constid,
  3421.             s.constid
  3422.         from
  3423.             sysreferences r, sysconstraints s
  3424.         where    r.rkeyid = s.id
  3425.             AND (s.status & 0xf) = 1
  3426.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3427.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3428.       union all
  3429.         select
  3430.             r.rkeydbid,
  3431.             r.rkeyid,
  3432.             r.rkey3,
  3433.             r.fkeydbid,
  3434.             r.fkeyid,
  3435.             r.fkey3,
  3436.             3,
  3437.             r.constid,
  3438.             s.constid
  3439.         from
  3440.             sysreferences r, sysconstraints s
  3441.         where    r.rkeyid = s.id
  3442.             AND (s.status & 0xf) = 1
  3443.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3444.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3445.       union all
  3446.         select
  3447.             r.rkeydbid,
  3448.             r.rkeyid,
  3449.             r.rkey4,
  3450.             r.fkeydbid,
  3451.             r.fkeyid,
  3452.             r.fkey4,
  3453.             4,
  3454.             r.constid,
  3455.             s.constid
  3456.         from
  3457.             sysreferences r, sysconstraints s
  3458.         where    r.rkeyid = s.id
  3459.             AND (s.status & 0xf) = 1
  3460.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3461.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3462.       union all
  3463.         select
  3464.             r.rkeydbid,
  3465.             r.rkeyid,
  3466.             r.rkey5,
  3467.             r.fkeydbid,
  3468.             r.fkeyid,
  3469.             r.fkey5,
  3470.             5,
  3471.             r.constid,
  3472.             s.constid
  3473.         from
  3474.             sysreferences r, sysconstraints s
  3475.         where    r.rkeyid = s.id
  3476.             AND (s.status & 0xf) = 1
  3477.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3478.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3479.       union all
  3480.         select
  3481.             r.rkeydbid,
  3482.             r.rkeyid,
  3483.             r.rkey6,
  3484.             r.fkeydbid,
  3485.             r.fkeyid,
  3486.             r.fkey6,
  3487.             6,
  3488.             r.constid,
  3489.             s.constid
  3490.         from
  3491.             sysreferences r, sysconstraints s
  3492.         where    r.rkeyid = s.id
  3493.             AND (s.status & 0xf) = 1
  3494.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3495.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3496.       union all
  3497.         select
  3498.             r.rkeydbid,
  3499.             r.rkeyid,
  3500.             r.rkey7,
  3501.             r.fkeydbid,
  3502.             r.fkeyid,
  3503.             r.fkey7,
  3504.             7,
  3505.             r.constid,
  3506.             s.constid
  3507.         from
  3508.             sysreferences r, sysconstraints s
  3509.         where    r.rkeyid = s.id
  3510.             AND (s.status & 0xf) = 1
  3511.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3512.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3513.       union all
  3514.         select
  3515.             r.rkeydbid,
  3516.             r.rkeyid,
  3517.             r.rkey8,
  3518.             r.fkeydbid,
  3519.             r.fkeyid,
  3520.             r.fkey8,
  3521.             8,
  3522.             r.constid,
  3523.             s.constid
  3524.         from
  3525.             sysreferences r, sysconstraints s
  3526.         where    r.rkeyid = s.id
  3527.             AND (s.status & 0xf) = 1
  3528.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3529.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3530.       union all
  3531.         select
  3532.             r.rkeydbid,
  3533.             r.rkeyid,
  3534.             r.rkey9,
  3535.             r.fkeydbid,
  3536.             r.fkeyid,
  3537.             r.fkey9,
  3538.             9,
  3539.             r.constid,
  3540.             s.constid
  3541.         from
  3542.             sysreferences r, sysconstraints s
  3543.         where    r.rkeyid = s.id
  3544.             AND (s.status & 0xf) = 1
  3545.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3546.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3547.       union all
  3548.         select
  3549.             r.rkeydbid,
  3550.             r.rkeyid,
  3551.             r.rkey10,
  3552.             r.fkeydbid,
  3553.             r.fkeyid,
  3554.             r.fkey10,
  3555.             10,
  3556.             r.constid,
  3557.             s.constid
  3558.         from
  3559.             sysreferences r, sysconstraints s
  3560.         where    r.rkeyid = s.id
  3561.             AND (s.status & 0xf) = 1
  3562.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3563.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3564.       union all
  3565.         select
  3566.             r.rkeydbid,
  3567.             r.rkeyid,
  3568.             r.rkey11,
  3569.             r.fkeydbid,
  3570.             r.fkeyid,
  3571.             r.fkey11,
  3572.             11,
  3573.             r.constid,
  3574.             s.constid
  3575.         from
  3576.             sysreferences r, sysconstraints s
  3577.         where    r.rkeyid = s.id
  3578.             AND (s.status & 0xf) = 1
  3579.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3580.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3581.       union all
  3582.         select
  3583.             r.rkeydbid,
  3584.             r.rkeyid,
  3585.             r.rkey12,
  3586.             r.fkeydbid,
  3587.             r.fkeyid,
  3588.             r.fkey12,
  3589.             12,
  3590.             r.constid,
  3591.             s.constid
  3592.         from
  3593.             sysreferences r, sysconstraints s
  3594.         where    r.rkeyid = s.id
  3595.             AND (s.status & 0xf) = 1
  3596.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3597.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3598.       union all
  3599.         select
  3600.             r.rkeydbid,
  3601.             r.rkeyid,
  3602.             r.rkey13,
  3603.             r.fkeydbid,
  3604.             r.fkeyid,
  3605.             r.fkey13,
  3606.             13,
  3607.             r.constid,
  3608.             s.constid
  3609.         from
  3610.             sysreferences r, sysconstraints s
  3611.         where    r.rkeyid = s.id
  3612.             AND (s.status & 0xf) = 1
  3613.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3614.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3615.       union all
  3616.         select
  3617.             r.rkeydbid,
  3618.             r.rkeyid,
  3619.             r.rkey14,
  3620.             r.fkeydbid,
  3621.             r.fkeyid,
  3622.             r.fkey14,
  3623.             14,
  3624.             r.constid,
  3625.             s.constid
  3626.         from
  3627.             sysreferences r, sysconstraints s
  3628.         where    r.rkeyid = s.id
  3629.             AND (s.status & 0xf) = 1
  3630.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3631.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3632.       union all
  3633.         select
  3634.             r.rkeydbid,
  3635.             r.rkeyid,
  3636.             r.rkey15,
  3637.             r.fkeydbid,
  3638.             r.fkeyid,
  3639.             r.fkey15,
  3640.             15,
  3641.             r.constid,
  3642.             s.constid
  3643.         from
  3644.             sysreferences r, sysconstraints s
  3645.         where    r.rkeyid = s.id
  3646.             AND (s.status & 0xf) = 1
  3647.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3648.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3649.       union all
  3650.         select
  3651.             r.rkeydbid,
  3652.             r.rkeyid,
  3653.             r.rkey16,
  3654.             r.fkeydbid,
  3655.             r.fkeyid,
  3656.             r.fkey16,
  3657.             16,
  3658.             r.constid,
  3659.             s.constid
  3660.         from
  3661.             sysreferences r, sysconstraints s
  3662.         where    r.rkeyid = s.id
  3663.             AND (s.status & 0xf) = 1
  3664.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3665.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3666.  
  3667.     if @order_by_pk = 1 /*    If order by PK fields */
  3668.         select
  3669.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3670.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3671.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3672.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3673.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3674.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3675.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3676.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3677.             KEY_SEQ,
  3678.             UPDATE_RULE = convert(smallint,1),
  3679.             DELETE_RULE = convert(smallint,1),
  3680.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3681.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3682.             DEFERRABILITY = convert(smallint,7)
  3683.         from #fkeys f,
  3684.             sysobjects o1, sysobjects o2,
  3685.             syscolumns c1, syscolumns c2
  3686.         where    o1.id = f.pktable_id
  3687.             AND o2.id = f.fktable_id
  3688.             AND c1.id = f.pktable_id
  3689.             AND c2.id = f.fktable_id
  3690.             AND c1.colid = f.pkcolid
  3691.             AND c2.colid = f.fkcolid
  3692.         order by 1,2,3,9,4
  3693.     else        /*    Order by FK fields */
  3694.         select
  3695.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3696.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3697.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3698.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3699.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3700.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3701.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3702.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3703.             KEY_SEQ,
  3704.             UPDATE_RULE = convert(smallint,1),
  3705.             DELETE_RULE = convert(smallint,1),
  3706.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3707.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3708.             DEFERRABILITY = convert(smallint,7)
  3709.         from #fkeys f,
  3710.             sysobjects o1, sysobjects o2,
  3711.             syscolumns c1, syscolumns c2
  3712.         where    o1.id = f.pktable_id
  3713.             AND o2.id = f.fktable_id
  3714.             AND c1.id = f.pktable_id
  3715.             AND c2.id = f.fktable_id
  3716.             AND c1.colid = f.pkcolid
  3717.             AND c2.colid = f.fkcolid
  3718.         order by 5,6,7,9,8
  3719. go
  3720.  
  3721. if (charindex('7.00', @@version) = 0 and
  3722.     charindex('8.00', @@version) = 0)
  3723. begin
  3724.     print ''
  3725.     print ''
  3726.     print 'Warning:'
  3727.     print 'you are installing the stored procedures '
  3728.     print 'on a pre 7.0 SQL Server.'
  3729.     print 'Ignore the following errors.'
  3730. end
  3731. else
  3732.     drop proc sp_fkeys
  3733. go
  3734.  
  3735. /*    Procedure for 7.0 server */
  3736. CREATE PROCEDURE sp_fkeys(
  3737.                @pktable_name        sysname = null,
  3738.                @pktable_owner        sysname = null,
  3739.                @pktable_qualifier    sysname = null,
  3740.                @fktable_name        sysname = null,
  3741.                @fktable_owner        sysname = null,
  3742.                @fktable_qualifier    sysname = null )
  3743. as
  3744.     set nocount on
  3745.     DECLARE @pktable_id            int
  3746.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3747.     DECLARE @fktable_id            int
  3748.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3749.     declare    @order_by_pk        int
  3750.  
  3751.     /* select 'XXX starting table creation' */
  3752.  
  3753.     create table #fkeysall(
  3754.             rkeyid int NOT NULL,
  3755.             rkey1 int NOT NULL,
  3756.                 rkey2 int NOT NULL,
  3757.                 rkey3 int NOT NULL,
  3758.                 rkey4 int NOT NULL,
  3759.                 rkey5 int NOT NULL,
  3760.                 rkey6 int NOT NULL,
  3761.                 rkey7 int NOT NULL,
  3762.                 rkey8 int NOT NULL,
  3763.                 rkey9 int NOT NULL,
  3764.                 rkey10 int NOT NULL,
  3765.                 rkey11 int NOT NULL,
  3766.                 rkey12 int NOT NULL,
  3767.                 rkey13 int NOT NULL,
  3768.                 rkey14 int NOT NULL,
  3769.                 rkey15 int NOT NULL,
  3770.                 rkey16 int NOT NULL,
  3771.             fkeyid int NOT NULL,
  3772.             fkey1 int NOT NULL,
  3773.                 fkey2 int NOT NULL,
  3774.                 fkey3 int NOT NULL,
  3775.                 fkey4 int NOT NULL,
  3776.                 fkey5 int NOT NULL,
  3777.                 fkey6 int NOT NULL,
  3778.                 fkey7 int NOT NULL,
  3779.                 fkey8 int NOT NULL,
  3780.                 fkey9 int NOT NULL,
  3781.                 fkey10 int NOT NULL,
  3782.                 fkey11 int NOT NULL,
  3783.                 fkey12 int NOT NULL,
  3784.                 fkey13 int NOT NULL,
  3785.                 fkey14 int NOT NULL,
  3786.                 fkey15 int NOT NULL,
  3787.                 fkey16 int NOT NULL,
  3788.             constid int NOT NULL,
  3789.             name sysname NOT NULL)
  3790.  
  3791.     create table #fkeys(
  3792.             pktable_id        int NOT NULL,
  3793.             pkcolid         int NOT NULL,
  3794.             fktable_id        int NOT NULL,
  3795.             fkcolid         int NOT NULL,
  3796.             KEY_SEQ         smallint NOT NULL,
  3797.             fk_id            int NOT NULL,
  3798.             PK_NAME            sysname NOT NULL)
  3799.  
  3800.     create table #fkeysout(
  3801.             PKTABLE_QUALIFIER sysname NULL,
  3802.             PKTABLE_OWNER sysname NULL,
  3803.             PKTABLE_NAME sysname NOT NULL,
  3804.             PKCOLUMN_NAME sysname NOT NULL,
  3805.             FKTABLE_QUALIFIER sysname NULL,
  3806.             FKTABLE_OWNER sysname NULL,
  3807.             FKTABLE_NAME sysname NOT NULL,
  3808.             FKCOLUMN_NAME sysname NOT NULL,
  3809.             KEY_SEQ smallint NOT NULL,
  3810.             UPDATE_RULE smallint NULL,
  3811.             DELETE_RULE smallint NULL,
  3812.             FK_NAME sysname NULL,
  3813.             PK_NAME sysname NULL,
  3814.             DEFERRABILITY smallint null)
  3815.  
  3816.     /* select 'XXX starting parameter analysis' */
  3817.  
  3818.     select  @order_by_pk = 0
  3819.  
  3820.     if (@pktable_name is null) and (@fktable_name is null)
  3821.     begin    /* If neither primary key nor foreign key table names given */
  3822.         raiserror (15252,-1,-1)
  3823.         return
  3824.     end
  3825.     if @fktable_qualifier is not null
  3826.     begin
  3827.         if db_name() <> @fktable_qualifier
  3828.         begin    /* If qualifier doesn't match current database */
  3829.             raiserror (15250, -1,-1)
  3830.             return
  3831.         end
  3832.     end
  3833.     if @pktable_qualifier is not null
  3834.     begin
  3835.         if db_name() <> @pktable_qualifier
  3836.         begin    /* If qualifier doesn't match current database */
  3837.             raiserror (15250, -1,-1)
  3838.             return
  3839.         end
  3840.     end
  3841.  
  3842.     if @pktable_owner is null
  3843.     begin    /* If unqualified primary key table name */
  3844.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3845.     end
  3846.     else
  3847.     begin    /* Qualified primary key table name */
  3848.         if @pktable_owner = ''
  3849.         begin    /* If empty owner name */
  3850.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3851.         end
  3852.         else
  3853.         begin
  3854.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3855.                 '.' + quotename(@pktable_name)
  3856.         end
  3857.     end
  3858.     /*    Get Object ID */
  3859.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3860.  
  3861.     if @fktable_owner is null
  3862.     begin    /* If unqualified foreign key table name */
  3863.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3864.     end
  3865.     else
  3866.     begin    /* Qualified foreign key table name */
  3867.         if @fktable_owner = ''
  3868.         begin    /* If empty owner name */
  3869.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3870.         end
  3871.         else
  3872.         begin
  3873.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3874.                 '.' + quotename(@fktable_name)
  3875.         end
  3876.     end
  3877.     /*    Get Object ID */
  3878.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3879.  
  3880.     if @fktable_name is not null
  3881.     begin
  3882.         if @fktable_id is null
  3883.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3884.     end
  3885.  
  3886.     if @pktable_name is null
  3887.     begin /*  If table name not supplied, match all */
  3888.         select @order_by_pk = 1
  3889.     end
  3890.     else
  3891.     begin
  3892.         if @pktable_id is null
  3893.         begin
  3894.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3895.         end
  3896.     end
  3897.  
  3898.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3899.     /*    Process syskeys for each relationship */
  3900.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3901.         them out with a 16-way "insert select ... union select ..." */
  3902.  
  3903.     /* select 'XXX starting data analysis' */
  3904.  
  3905.     insert into #fkeysall
  3906.         select
  3907.             r.rkeyid,
  3908.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3909.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3910.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3911.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3912.             r.fkeyid,
  3913.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3914.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3915.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3916.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3917.             r.constid,
  3918.             i.name
  3919.         from
  3920.             sysreferences r, sysobjects o, sysindexes i
  3921.         where    r.constid = o.id
  3922.             AND o.xtype = 'F'
  3923.             AND r.rkeyindid = i.indid
  3924.             AND r.rkeyid = i.id
  3925.             AND r.rkeyid between isnull(@pktable_id, 0)
  3926.                             and isnull(@pktable_id, 0x7fffffff)
  3927.             AND r.fkeyid between isnull(@fktable_id, 0)
  3928.                             and isnull(@fktable_id, 0x7fffffff)
  3929.  
  3930.     /* select count (*) as 'XXX countall' from #fkeysall */
  3931.  
  3932.     insert into #fkeys
  3933.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3934.             from #fkeysall
  3935.         union all
  3936.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3937.             from #fkeysall
  3938.         union all
  3939.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3940.             from #fkeysall
  3941.         union all
  3942.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3943.             from #fkeysall
  3944.         union all
  3945.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3946.             from #fkeysall
  3947.         union all
  3948.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3949.             from #fkeysall
  3950.         union all
  3951.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3952.             from #fkeysall
  3953.         union all
  3954.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3955.             from #fkeysall
  3956.         union all
  3957.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3958.             from #fkeysall
  3959.         union all
  3960.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3961.             from #fkeysall
  3962.         union all
  3963.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3964.             from #fkeysall
  3965.         union all
  3966.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3967.             from #fkeysall
  3968.         union all
  3969.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3970.             from #fkeysall
  3971.         union all
  3972.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3973.             from #fkeysall
  3974.         union all
  3975.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3976.             from #fkeysall
  3977.         union all
  3978.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3979.             from #fkeysall
  3980.  
  3981.     /* select count (*) as 'XXX count' from #fkeys */
  3982.  
  3983.     insert into #fkeysout
  3984.         select
  3985.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3986.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3987.             PKTABLE_NAME = convert(sysname,o1.name),
  3988.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3989.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3990.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3991.             FKTABLE_NAME = convert(sysname,o2.name),
  3992.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3993.             KEY_SEQ,
  3994.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  3995.                 convert(smallint,0) ELSE convert(smallint,1) END,
  3996.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  3997.                 convert(smallint,0) ELSE convert(smallint,1) END,
  3998.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  3999.             PK_NAME,
  4000.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  4001.         from #fkeys f,
  4002.             sysobjects o1, sysobjects o2,
  4003.             syscolumns c1, syscolumns c2
  4004.         where    o1.id = f.pktable_id
  4005.             AND o2.id = f.fktable_id
  4006.             AND c1.id = f.pktable_id
  4007.             AND c2.id = f.fktable_id
  4008.             AND c1.colid = f.pkcolid
  4009.             AND c2.colid = f.fkcolid
  4010.     /* select count (*) as 'XXX countout' from #fkeysout */
  4011.  
  4012.     if @order_by_pk = 1 /*    If order by PK fields */
  4013.         select
  4014.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4015.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4016.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4017.         from #fkeysout
  4018.         order by 1,2,3,9,4
  4019.     else        /*    Order by FK fields */
  4020.         select
  4021.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4022.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4023.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4024.         from #fkeysout
  4025.         order by 5,6,7,9,8
  4026. go
  4027.  
  4028. if (charindex('8.00', @@version) = 0)
  4029. begin
  4030.     print ''
  4031.     print ''
  4032.     print 'Warning:'
  4033.     print 'you are installing the stored procedures '
  4034.     print 'on a pre 8.0 SQL Server.'
  4035.     print 'Ignore the following errors.'
  4036. end
  4037. else
  4038.     drop proc sp_fkeys
  4039. go
  4040.  
  4041. /*    Procedure for 8.0 server */
  4042. CREATE PROCEDURE sp_fkeys(
  4043.                @pktable_name        sysname = null,
  4044.                @pktable_owner        sysname = null,
  4045.                @pktable_qualifier    sysname = null,
  4046.                @fktable_name        sysname = null,
  4047.                @fktable_owner        sysname = null,
  4048.                @fktable_qualifier    sysname = null )
  4049. as
  4050.     set nocount on
  4051.     DECLARE @pktable_id            int
  4052.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  4053.     DECLARE @fktable_id            int
  4054.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  4055.     declare    @order_by_pk        int
  4056.  
  4057.     /* select 'XXX starting table creation' */
  4058.  
  4059.     create table #fkeysall(
  4060.             rkeyid int NOT NULL,
  4061.             rkey1 int NOT NULL,
  4062.                 rkey2 int NOT NULL,
  4063.                 rkey3 int NOT NULL,
  4064.                 rkey4 int NOT NULL,
  4065.                 rkey5 int NOT NULL,
  4066.                 rkey6 int NOT NULL,
  4067.                 rkey7 int NOT NULL,
  4068.                 rkey8 int NOT NULL,
  4069.                 rkey9 int NOT NULL,
  4070.                 rkey10 int NOT NULL,
  4071.                 rkey11 int NOT NULL,
  4072.                 rkey12 int NOT NULL,
  4073.                 rkey13 int NOT NULL,
  4074.                 rkey14 int NOT NULL,
  4075.                 rkey15 int NOT NULL,
  4076.                 rkey16 int NOT NULL,
  4077.             fkeyid int NOT NULL,
  4078.             fkey1 int NOT NULL,
  4079.                 fkey2 int NOT NULL,
  4080.                 fkey3 int NOT NULL,
  4081.                 fkey4 int NOT NULL,
  4082.                 fkey5 int NOT NULL,
  4083.                 fkey6 int NOT NULL,
  4084.                 fkey7 int NOT NULL,
  4085.                 fkey8 int NOT NULL,
  4086.                 fkey9 int NOT NULL,
  4087.                 fkey10 int NOT NULL,
  4088.                 fkey11 int NOT NULL,
  4089.                 fkey12 int NOT NULL,
  4090.                 fkey13 int NOT NULL,
  4091.                 fkey14 int NOT NULL,
  4092.                 fkey15 int NOT NULL,
  4093.                 fkey16 int NOT NULL,
  4094.             constid int NOT NULL,
  4095.             name sysname collate database_default NOT NULL)
  4096.  
  4097.     create table #fkeys(
  4098.             pktable_id        int NOT NULL,
  4099.             pkcolid         int NOT NULL,
  4100.             fktable_id        int NOT NULL,
  4101.             fkcolid         int NOT NULL,
  4102.             KEY_SEQ         smallint NOT NULL,
  4103.             fk_id            int NOT NULL,
  4104.             PK_NAME            sysname collate database_default NOT NULL)
  4105.  
  4106.     create table #fkeysout(
  4107.             PKTABLE_QUALIFIER sysname collate database_default NULL,
  4108.             PKTABLE_OWNER sysname collate database_default NULL,
  4109.             PKTABLE_NAME sysname collate database_default NOT NULL,
  4110.             PKCOLUMN_NAME sysname collate database_default NOT NULL,
  4111.             FKTABLE_QUALIFIER sysname collate database_default NULL,
  4112.             FKTABLE_OWNER sysname collate database_default NULL,
  4113.             FKTABLE_NAME sysname collate database_default NOT NULL,
  4114.             FKCOLUMN_NAME sysname collate database_default NOT NULL,
  4115.             KEY_SEQ smallint NOT NULL,
  4116.             UPDATE_RULE smallint NULL,
  4117.             DELETE_RULE smallint NULL,
  4118.             FK_NAME sysname collate database_default NULL,
  4119.             PK_NAME sysname collate database_default NULL,
  4120.             DEFERRABILITY smallint null)
  4121.  
  4122.     /* select 'XXX starting parameter analysis' */
  4123.  
  4124.     select  @order_by_pk = 0
  4125.  
  4126.     if (@pktable_name is null) and (@fktable_name is null)
  4127.     begin    /* If neither primary key nor foreign key table names given */
  4128.         raiserror (15252,-1,-1)
  4129.         return
  4130.     end
  4131.     if @fktable_qualifier is not null
  4132.     begin
  4133.         if db_name() <> @fktable_qualifier
  4134.         begin    /* If qualifier doesn't match current database */
  4135.             raiserror (15250, -1,-1)
  4136.             return
  4137.         end
  4138.     end
  4139.     if @pktable_qualifier is not null
  4140.     begin
  4141.         if db_name() <> @pktable_qualifier
  4142.         begin    /* If qualifier doesn't match current database */
  4143.             raiserror (15250, -1,-1)
  4144.             return
  4145.         end
  4146.     end
  4147.  
  4148.     if @pktable_owner is null
  4149.     begin    /* If unqualified primary key table name */
  4150.         SELECT @pkfull_table_name = quotename(@pktable_name)
  4151.     end
  4152.     else
  4153.     begin    /* Qualified primary key table name */
  4154.         if @pktable_owner = ''
  4155.         begin    /* If empty owner name */
  4156.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  4157.         end
  4158.         else
  4159.         begin
  4160.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  4161.                 '.' + quotename(@pktable_name)
  4162.         end
  4163.     end
  4164.     /*    Get Object ID */
  4165.     SELECT @pktable_id = object_id(@pkfull_table_name)
  4166.  
  4167.     if @fktable_owner is null
  4168.     begin    /* If unqualified foreign key table name */
  4169.         SELECT @fkfull_table_name = quotename(@fktable_name)
  4170.     end
  4171.     else
  4172.     begin    /* Qualified foreign key table name */
  4173.         if @fktable_owner = ''
  4174.         begin    /* If empty owner name */
  4175.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  4176.         end
  4177.         else
  4178.         begin
  4179.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  4180.                 '.' + quotename(@fktable_name)
  4181.         end
  4182.     end
  4183.     /*    Get Object ID */
  4184.     SELECT @fktable_id = object_id(@fkfull_table_name)
  4185.  
  4186.     if @fktable_name is not null
  4187.     begin
  4188.         if @fktable_id is null
  4189.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  4190.     end
  4191.  
  4192.     if @pktable_name is null
  4193.     begin /*  If table name not supplied, match all */
  4194.         select @order_by_pk = 1
  4195.     end
  4196.     else
  4197.     begin
  4198.         if @pktable_id is null
  4199.         begin
  4200.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  4201.         end
  4202.     end
  4203.  
  4204.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  4205.     /*    Process syskeys for each relationship */
  4206.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  4207.         them out with a 16-way "insert select ... union select ..." */
  4208.  
  4209.     /* select 'XXX starting data analysis' */
  4210.  
  4211.     insert into #fkeysall
  4212.         select
  4213.             r.rkeyid,
  4214.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  4215.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  4216.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  4217.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  4218.             r.fkeyid,
  4219.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  4220.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  4221.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  4222.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  4223.             r.constid,
  4224.             i.name
  4225.         from
  4226.             sysreferences r, sysobjects o, sysindexes i
  4227.         where    r.constid = o.id
  4228.             AND o.xtype = 'F'
  4229.             AND r.rkeyindid = i.indid
  4230.             AND r.rkeyid = i.id
  4231.             AND r.rkeyid between isnull(@pktable_id, 0)
  4232.                             and isnull(@pktable_id, 0x7fffffff)
  4233.             AND r.fkeyid between isnull(@fktable_id, 0)
  4234.                             and isnull(@fktable_id, 0x7fffffff)
  4235.  
  4236.     /* select count (*) as 'XXX countall' from #fkeysall */
  4237.  
  4238.     insert into #fkeys
  4239.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  4240.             from #fkeysall
  4241.         union all
  4242.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  4243.             from #fkeysall
  4244.         union all
  4245.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  4246.             from #fkeysall
  4247.         union all
  4248.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  4249.             from #fkeysall
  4250.         union all
  4251.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  4252.             from #fkeysall
  4253.         union all
  4254.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  4255.             from #fkeysall
  4256.         union all
  4257.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  4258.             from #fkeysall
  4259.         union all
  4260.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  4261.             from #fkeysall
  4262.         union all
  4263.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  4264.             from #fkeysall
  4265.         union all
  4266.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  4267.             from #fkeysall
  4268.         union all
  4269.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  4270.             from #fkeysall
  4271.         union all
  4272.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  4273.             from #fkeysall
  4274.         union all
  4275.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  4276.             from #fkeysall
  4277.         union all
  4278.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  4279.             from #fkeysall
  4280.         union all
  4281.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  4282.             from #fkeysall
  4283.         union all
  4284.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  4285.             from #fkeysall
  4286.  
  4287.     /* select count (*) as 'XXX count' from #fkeys */
  4288.  
  4289.     insert into #fkeysout
  4290.         select
  4291.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  4292.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  4293.             PKTABLE_NAME = convert(sysname,o1.name),
  4294.             PKCOLUMN_NAME = convert(sysname,c1.name),
  4295.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  4296.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  4297.             FKTABLE_NAME = convert(sysname,o2.name),
  4298.             FKCOLUMN_NAME = convert(sysname,c2.name),
  4299.             KEY_SEQ,
  4300.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  4301.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4302.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  4303.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4304.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  4305.             PK_NAME,
  4306.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  4307.         from #fkeys f,
  4308.             sysobjects o1, sysobjects o2,
  4309.             syscolumns c1, syscolumns c2
  4310.         where    o1.id = f.pktable_id
  4311.             AND o2.id = f.fktable_id
  4312.             AND c1.id = f.pktable_id
  4313.             AND c2.id = f.fktable_id
  4314.             AND c1.colid = f.pkcolid
  4315.             AND c2.colid = f.fkcolid
  4316.     /* select count (*) as 'XXX countout' from #fkeysout */
  4317.  
  4318.     if @order_by_pk = 1 /*    If order by PK fields */
  4319.         select
  4320.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4321.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4322.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4323.         from #fkeysout
  4324.         order by 1,2,3,9,4
  4325.     else        /*    Order by FK fields */
  4326.         select
  4327.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4328.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4329.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4330.         from #fkeysout
  4331.         order by 5,6,7,9,8
  4332. go
  4333. grant execute on sp_fkeys to public
  4334. go
  4335.  
  4336. dump tran master with no_log
  4337. go
  4338.  
  4339. print 'creating sp_pkeys'
  4340. go
  4341.  
  4342. /*    Procedure for pre-6.0 server */
  4343. CREATE PROCEDURE sp_pkeys(
  4344.                @table_name        varchar(32),
  4345.                @table_owner     varchar(32) = null,
  4346.                @table_qualifier varchar(32) = null )
  4347. as
  4348.     set nocount on
  4349.     if @table_qualifier is not null
  4350.     begin
  4351.         if db_name() <> @table_qualifier
  4352.         begin    /* If qualifier doesn't match current database */
  4353.             raiserror 20001 '~~Rush_5~~'
  4354.             return
  4355.         end
  4356.     end
  4357.     if @table_owner is null /*    If owner not supplied, match all */
  4358.         select @table_owner = '%'
  4359.     if @@trancount <> 0
  4360.     begin    /* If inside a transaction */
  4361.         raiserror 20003 '~~Rush_35~~'
  4362.         return
  4363.     end
  4364.  
  4365.     create table #pkeys(
  4366.              TABLE_QUALIFIER varchar(32) NULL,
  4367.              TABLE_OWNER     varchar(32) NULL,
  4368.              TABLE_NAME      varchar(32) NOT NULL,
  4369.              COLUMN_NAME     varchar(32) NOT NULL,
  4370.              KEY_SEQ         smallint NOT NULL)
  4371.  
  4372.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  4373.     /*    Process syskeys for each relationship */
  4374.     /*    The inserts below adds a row to the temp table for each of the
  4375.         8 possible relationships */
  4376.     insert into #pkeys
  4377.         select
  4378.             db_name(),
  4379.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4380.             object_name(k.id),
  4381.             c.name,
  4382.             1
  4383.         from
  4384.             syskeys k, syscolumns c
  4385.         where
  4386.             c.id = k.id
  4387.             and k.type = 1    /* Primary type key */
  4388.             and c.colid = k.key1
  4389.     if (@@rowcount = 0)
  4390.         goto done
  4391.  
  4392.     insert into #pkeys
  4393.         select
  4394.             db_name(),
  4395.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4396.             object_name(k.id),
  4397.             c.name,
  4398.             2
  4399.         from
  4400.             syskeys k, syscolumns c
  4401.         where
  4402.             c.id = k.id
  4403.             and k.type = 1    /* Primary type key */
  4404.             and c.colid = key2
  4405.     if (@@rowcount = 0)
  4406.         goto done
  4407.  
  4408.     insert into #pkeys
  4409.         select
  4410.             db_name(),
  4411.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4412.             object_name(k.id),
  4413.             c.name,
  4414.             3
  4415.         from
  4416.             syskeys k, syscolumns c
  4417.         where
  4418.             c.id = k.id
  4419.             and k.type = 1    /* Primary type key */
  4420.             and c.colid = key3
  4421.     if (@@rowcount = 0)
  4422.         goto done
  4423.  
  4424.     insert into #pkeys
  4425.         select
  4426.             db_name(),
  4427.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4428.             object_name(k.id),
  4429.             c.name,
  4430.             4
  4431.         from
  4432.             syskeys k, syscolumns c
  4433.         where
  4434.             c.id = k.id
  4435.             and k.type = 1    /* Primary type key */
  4436.             and c.colid = key4
  4437.     if (@@rowcount = 0)
  4438.         goto done
  4439.  
  4440.     insert into #pkeys
  4441.         select
  4442.             db_name(),
  4443.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4444.             object_name(k.id),
  4445.             c.name,
  4446.             5
  4447.         from
  4448.             syskeys k, syscolumns c
  4449.         where
  4450.             c.id = k.id
  4451.             and k.type = 1    /* Primary type key */
  4452.             and c.colid = key5
  4453.     if (@@rowcount = 0)
  4454.         goto done
  4455.  
  4456.     insert into #pkeys
  4457.         select
  4458.             db_name(),
  4459.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4460.             object_name(k.id),
  4461.             c.name,
  4462.             6
  4463.         from
  4464.             syskeys k, syscolumns c
  4465.         where
  4466.             c.id = k.id
  4467.             and k.type = 1    /* Primary type key */
  4468.             and c.colid = key6
  4469.     if (@@rowcount = 0)
  4470.         goto done
  4471.  
  4472.     insert into #pkeys
  4473.         select
  4474.             db_name(),
  4475.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4476.             object_name(k.id),
  4477.             c.name,
  4478.             7
  4479.         from
  4480.             syskeys k, syscolumns c
  4481.         where
  4482.             c.id = k.id
  4483.             and k.type = 1    /* Primary type key */
  4484.             and c.colid = key7
  4485.     if (@@rowcount = 0)
  4486.         goto done
  4487.  
  4488.     insert into #pkeys
  4489.          select
  4490.              db_name(),
  4491.              (select user_name(uid) from sysobjects o where o.id = k.id),
  4492.              object_name(k.id),
  4493.              c.name,
  4494.              8
  4495.          from
  4496.              syskeys k, syscolumns c
  4497.          where
  4498.              c.id = k.id
  4499.              and k.type = 1 /* Primary type key */
  4500.              and c.colid = key8
  4501.  
  4502.     done:
  4503.     select
  4504.         TABLE_QUALIFIER,
  4505.         TABLE_OWNER,
  4506.         TABLE_NAME,
  4507.         COLUMN_NAME,
  4508.         KEY_SEQ,
  4509.         PK_NAME = convert(varchar(32),null)
  4510.     from #pkeys
  4511.     where TABLE_NAME = @table_name
  4512.         and TABLE_OWNER like @table_owner
  4513.     order by 1, 2, 3, 5
  4514. go
  4515.  
  4516. if (charindex('6.00', @@version) = 0 and
  4517.     charindex('6.50', @@version) = 0 and
  4518.     charindex('7.00', @@version) = 0 and
  4519.     charindex('8.00', @@version) = 0)
  4520. begin
  4521.     print ''
  4522.     print ''
  4523.     print 'Warning:'
  4524.     print 'you are installing the stored procedures '
  4525.     print 'on a pre 6.0 SQL Server.'
  4526.     print 'Ignore the following error.'
  4527. end
  4528. else
  4529.     drop proc sp_pkeys
  4530. go
  4531.  
  4532. /*    Procedure for 6.0 and 6.50 servers */
  4533. CREATE PROCEDURE sp_pkeys(
  4534.                @table_name        sysname,
  4535.                @table_owner     sysname = null,
  4536.                @table_qualifier sysname = null )
  4537. as
  4538.     DECLARE @table_id            int
  4539.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  4540.  
  4541.     if @table_qualifier is not null
  4542.     begin
  4543.         if db_name() <> @table_qualifier
  4544.         begin    /* If qualifier doesn't match current database */
  4545.             raiserror (15250, -1,-1)
  4546.             return
  4547.         end
  4548.     end
  4549.     if @table_owner is null
  4550.     begin    /* If unqualified table name */
  4551.         SELECT @full_table_name = @table_name
  4552.     end
  4553.     else
  4554.     begin    /* Qualified table name */
  4555.         if @table_owner = ''
  4556.         begin    /* If empty owner name */
  4557.             SELECT @full_table_name = @table_owner
  4558.         end
  4559.         else
  4560.         begin
  4561.             SELECT @full_table_name = @table_owner + '.' + @table_name
  4562.         end
  4563.     end
  4564.     /*    Get Object ID */
  4565.     SELECT @table_id = object_id(@full_table_name)
  4566.  
  4567.     select
  4568.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4569.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4570.         TABLE_NAME = convert(sysname,o.name),
  4571.         COLUMN_NAME = convert(sysname,c.name),
  4572.         KEY_SEQ = convert(smallint,c1.colid),
  4573.         PK_NAME = convert(sysname,i.name)
  4574.     from
  4575.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  4576.     where
  4577.         o.id = @table_id
  4578.         and o.id = c.id
  4579.         and o.id = i.id
  4580.         and (i.status & 0x800) = 0x800
  4581.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4582.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4583.         and c1.id = @table_id
  4584.     order by 1, 2, 3, 5
  4585. go
  4586.  
  4587. if (charindex('7.00', @@version) = 0 and
  4588.     charindex('8.00', @@version) = 0)
  4589. begin
  4590.     print ''
  4591.     print ''
  4592.     print 'Warning:'
  4593.     print 'you are installing the stored procedures '
  4594.     print 'on a pre 7.0 SQL Server.'
  4595.     print 'Ignore the following errors.'
  4596. end
  4597. else
  4598.     drop proc sp_pkeys
  4599. go
  4600.  
  4601. /*    Procedure for 8.0 server */
  4602. CREATE PROCEDURE sp_pkeys(
  4603.                @table_name        sysname,
  4604.                @table_owner     sysname = null,
  4605.                @table_qualifier sysname = null )
  4606. as
  4607.     DECLARE @table_id        int
  4608.     DECLARE @full_table_name    nvarchar(255)
  4609.  
  4610.     if @table_qualifier is not null
  4611.     begin
  4612.         if db_name() <> @table_qualifier
  4613.         begin    /* If qualifier doesn't match current database */
  4614.             raiserror (15250, -1,-1)
  4615.             return
  4616.         end
  4617.     end
  4618.     if @table_owner is null
  4619.     begin    /* If unqualified table name */
  4620.         SELECT @full_table_name = quotename(@table_name)
  4621.     end
  4622.     else
  4623.     begin    /* Qualified table name */
  4624.         if @table_owner = ''
  4625.         begin    /* If empty owner name */
  4626.             SELECT @full_table_name = quotename(@table_owner)
  4627.         end
  4628.         else
  4629.         begin
  4630.             SELECT @full_table_name = quotename(@table_owner) +
  4631.                 '.' + quotename(@table_name)
  4632.         end
  4633.     end
  4634.     /*    Get Object ID */
  4635.     SELECT @table_id = object_id(@full_table_name)
  4636.  
  4637.     select
  4638.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4639.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4640.         TABLE_NAME = convert(sysname,o.name),
  4641.         COLUMN_NAME = convert(sysname,c.name),
  4642.         --KEY_SEQ = convert(smallint,c.colid),
  4643.         KEY_SEQ =
  4644.             case
  4645.                 when c.name = index_col(@full_table_name, i.indid,  1) then convert (smallint,1)
  4646.                 when c.name = index_col(@full_table_name, i.indid,  2) then convert (smallint,2)
  4647.                 when c.name = index_col(@full_table_name, i.indid,  3) then convert (smallint,3)
  4648.                 when c.name = index_col(@full_table_name, i.indid,  4) then convert (smallint,4)
  4649.                 when c.name = index_col(@full_table_name, i.indid,  5) then convert (smallint,5)
  4650.                 when c.name = index_col(@full_table_name, i.indid,  6) then convert (smallint,6)
  4651.                 when c.name = index_col(@full_table_name, i.indid,  7) then convert (smallint,7)
  4652.                 when c.name = index_col(@full_table_name, i.indid,  8) then convert (smallint,8)
  4653.                 when c.name = index_col(@full_table_name, i.indid,  9) then convert (smallint,9)
  4654.                 when c.name = index_col(@full_table_name, i.indid, 10) then convert (smallint,10)
  4655.                 when c.name = index_col(@full_table_name, i.indid, 11) then convert (smallint,11)
  4656.                 when c.name = index_col(@full_table_name, i.indid, 12) then convert (smallint,12)
  4657.                 when c.name = index_col(@full_table_name, i.indid, 13) then convert (smallint,13)
  4658.                 when c.name = index_col(@full_table_name, i.indid, 14) then convert (smallint,14)
  4659.                 when c.name = index_col(@full_table_name, i.indid, 15) then convert (smallint,15)
  4660.                 when c.name = index_col(@full_table_name, i.indid, 16) then convert (smallint,16)
  4661.             end,
  4662.         PK_NAME = convert(sysname,i.name)
  4663.     from
  4664.         sysindexes i, syscolumns c, sysobjects o --, syscolumns c1
  4665.     where
  4666.         o.id = @table_id
  4667.         and o.id = c.id
  4668.         and o.id = i.id
  4669.         and (i.status & 0x800) = 0x800
  4670.         --and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4671.         and (c.name = index_col (@full_table_name, i.indid,  1) or
  4672.              c.name = index_col (@full_table_name, i.indid,  2) or
  4673.              c.name = index_col (@full_table_name, i.indid,  3) or
  4674.              c.name = index_col (@full_table_name, i.indid,  4) or
  4675.              c.name = index_col (@full_table_name, i.indid,  5) or
  4676.              c.name = index_col (@full_table_name, i.indid,  6) or
  4677.              c.name = index_col (@full_table_name, i.indid,  7) or
  4678.              c.name = index_col (@full_table_name, i.indid,  8) or
  4679.              c.name = index_col (@full_table_name, i.indid,  9) or
  4680.              c.name = index_col (@full_table_name, i.indid, 10) or
  4681.              c.name = index_col (@full_table_name, i.indid, 11) or
  4682.              c.name = index_col (@full_table_name, i.indid, 12) or
  4683.              c.name = index_col (@full_table_name, i.indid, 13) or
  4684.              c.name = index_col (@full_table_name, i.indid, 14) or
  4685.              c.name = index_col (@full_table_name, i.indid, 15) or
  4686.              c.name = index_col (@full_table_name, i.indid, 16)
  4687.             )
  4688.         --and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4689.         --and c1.id = @table_id
  4690.     order by 1, 2, 3, 5
  4691. go
  4692.  
  4693.  
  4694. grant execute on sp_pkeys to public
  4695. go
  4696.  
  4697. dump tran master with no_log
  4698. go
  4699.  
  4700. print 'creating sp_server_info'
  4701. go
  4702.  
  4703. create proc sp_server_info (
  4704.             @attribute_id  int = null)
  4705. as
  4706.     if @attribute_id is not null
  4707.         select *
  4708.         from master.dbo.spt_server_info
  4709.         where attribute_id = @attribute_id
  4710.     else
  4711.         select *
  4712.         from master.dbo.spt_server_info
  4713.         order by attribute_id
  4714. go
  4715.  
  4716. grant execute on sp_server_info to public
  4717. go
  4718.  
  4719. dump tran master with no_log
  4720. go
  4721.  
  4722. print 'creating sp_special_columns'
  4723. go
  4724.  
  4725. /*    Procedure for pre-6.0 server */
  4726. CREATE PROCEDURE sp_special_columns (
  4727.                  @table_name        varchar(32),
  4728.                  @table_owner        varchar(32) = null,
  4729.                  @table_qualifier    varchar(32) = null,
  4730.                  @col_type            char(1) = 'R',
  4731.                  @scope                char(1) = 'T',
  4732.                  @nullable            char(1) = 'U',
  4733.                  @ODBCVer            int = 2)
  4734. AS
  4735.     DECLARE @indid                int
  4736.     DECLARE @table_id            int
  4737.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4738.     DECLARE @scopeout            smallint
  4739.  
  4740.     if @col_type not in ('R','V') or @col_type is null
  4741.     begin
  4742.         raiserror 20002 '~~Rush_42~~'
  4743.         return
  4744.     end
  4745.  
  4746.     if @scope = 'C'
  4747.         select @scopeout = 0
  4748.     else if @scope = 'T'
  4749.         select @scopeout = 1
  4750.     else
  4751.     begin
  4752.         raiserror 20002 '~~Rush_43~~'
  4753.         return
  4754.     end
  4755.  
  4756.     if @nullable not in ('U','O') or @nullable is null
  4757.     begin
  4758.         raiserror 20002 '~~Rush_44~~'
  4759.         return
  4760.     end
  4761.  
  4762.     if @table_qualifier is not null
  4763.     begin
  4764.         if db_name() <> @table_qualifier
  4765.         begin /* If qualifier doesn't match current database */
  4766.             raiserror 20001 '~~Rush_5~~'
  4767.             return
  4768.         end
  4769.     end
  4770.     if @table_owner is null
  4771.     begin     /* If unqualified table name */
  4772.         SELECT @full_table_name = @table_name
  4773.     end
  4774.     else
  4775.     begin    /* Qualified table name */
  4776.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4777.     end
  4778.     /*    Get Object ID */
  4779.     SELECT @table_id = object_id(@full_table_name)
  4780.  
  4781.     if @col_type = 'V'
  4782.     BEGIN /* if ROWVER, just run that query */
  4783.         SELECT
  4784.             SCOPE = convert(smallint,NULL),
  4785.             COLUMN_NAME = convert(varchar(32),c.name),
  4786.             DATA_TYPE = convert(smallint, -3),
  4787.             TYPE_NAME = t.name,
  4788.             "PRECISION" = convert(int,8),
  4789.             LENGTH = convert(int,8),
  4790.             SCALE = convert(smallint, NULL),
  4791.             PSEUDO_COLUMN = convert(smallint,1)
  4792.         FROM
  4793.             systypes t, syscolumns c
  4794.         WHERE
  4795.             c.id = @table_id
  4796.             AND c.usertype = 80 /*    TIMESTAMP */
  4797.             AND t.usertype = 80 /*    TIMESTAMP */
  4798.         RETURN
  4799.     END
  4800.  
  4801.     /* ROWID, now find the id of the 'best' index for this table */
  4802.  
  4803.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4804.                            nullable columns. */
  4805.  
  4806.             SELECT @indid = MIN(indid)
  4807.                 FROM sysindexes i,syscolumns c,syscolumns c2
  4808.                 WHERE
  4809.                     i.status&2 = 2        /*    If Unique Index */
  4810.                      AND c.id = i.id
  4811.                      AND c2.id = c.id
  4812.                      AND c2.colid < i.keycnt + (i.status&16)/16
  4813.                     AND i.id = @table_id
  4814.                     AND indid > 0        /*    Eliminate Table Row */
  4815.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  4816.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  4817.  
  4818.     ELSE    /* Include indexes that are partially nullable. */
  4819.  
  4820.         SELECT @indid = MIN(indid)
  4821.             FROM sysindexes i
  4822.             WHERE
  4823.                 status&2 = 2        /*    If Unique Index */
  4824.                 AND id = @table_id
  4825.                 AND indid > 0        /*    Eliminate Table Row */
  4826.  
  4827.     SELECT
  4828.         SCOPE = @scopeout,
  4829.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4830.         d.DATA_TYPE,
  4831.         TYPE_NAME = t.name,
  4832.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4833.         LENGTH = isnull(d.length, convert(int,c.length)),
  4834.         SCALE = d.numeric_scale,
  4835.         PSEUDO_COLUMN = convert(smallint,1)
  4836.     FROM
  4837.         sysindexes x,
  4838.         syscolumns c,
  4839.         master.dbo.spt_datatype_info d,
  4840.         systypes t,
  4841.         syscolumns c2    /* Self-join to generate list of index columns and */
  4842.                         /* to extract datatype names */
  4843.     WHERE
  4844.         x.id = @table_id
  4845.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4846.         AND c.id = x.id
  4847.         AND c2.id = x.id
  4848.         AND c2.colid < keycnt+(x.status&16)/16
  4849.         AND x.indid = @indid
  4850.         AND t.type = d.ss_dtype
  4851.         AND c.length = d.fixlen
  4852.         AND c.usertype = t.usertype
  4853.  
  4854. go
  4855.  
  4856. if (charindex('6.00', @@version) = 0 and
  4857.     charindex('6.50', @@version) = 0 and
  4858.     charindex('7.00', @@version) = 0 and
  4859.     charindex('8.00', @@version) = 0)
  4860. begin
  4861.     print ''
  4862.     print ''
  4863.     print 'Warning:'
  4864.     print 'you are installing the stored procedures '
  4865.     print 'on a pre 6.0 SQL Server.'
  4866.     print 'Ignore the following errors.'
  4867. end
  4868. else
  4869.     drop proc sp_special_columns
  4870. go
  4871.  
  4872. /*    Procedure for 6.0 and 6.50 servers */
  4873. CREATE PROCEDURE sp_special_columns (
  4874.                  @table_name        varchar(32),
  4875.                  @table_owner        varchar(32) = null,
  4876.                  @table_qualifier    varchar(32) = null,
  4877.                  @col_type            char(1) = 'R',
  4878.                  @scope                char(1) = 'T',
  4879.                  @nullable            char(1) = 'U',
  4880.                  @ODBCVer            int = 2)
  4881. AS
  4882.     DECLARE @indid                int
  4883.     DECLARE @table_id            int
  4884.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4885.     DECLARE @scopeout            smallint
  4886.  
  4887.     if @col_type not in ('R','V') or @col_type is null
  4888.     begin
  4889.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4890.         return
  4891.     end
  4892.  
  4893.     if @scope = 'C'
  4894.         select @scopeout = 0
  4895.     else if @scope = 'T'
  4896.         select @scopeout = 1
  4897.     else
  4898.     begin
  4899.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4900.         return
  4901.     end
  4902.  
  4903.     if @nullable not in ('U','O') or @nullable is null
  4904.     begin
  4905.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4906.         return
  4907.     end
  4908.  
  4909.     if @table_qualifier is not null
  4910.     begin
  4911.         if db_name() <> @table_qualifier
  4912.         begin /* If qualifier doesn't match current database */
  4913.             raiserror (15250, -1,-1)
  4914.             return
  4915.         end
  4916.     end
  4917.     if @table_owner is null
  4918.     begin     /* If unqualified table name */
  4919.         SELECT @full_table_name = @table_name
  4920.     end
  4921.     else
  4922.     begin     /* Qualified table name */
  4923.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4924.     end
  4925.     /*    Get Object ID */
  4926.     SELECT @table_id = object_id(@full_table_name)
  4927.  
  4928.     if @col_type = 'V'
  4929.     BEGIN /* if ROWVER, just run that query */
  4930.         SELECT
  4931.             SCOPE = convert(smallint,NULL),
  4932.             COLUMN_NAME = convert(varchar(32),c.name),
  4933.             DATA_TYPE = convert(smallint, -2),
  4934.             TYPE_NAME = t.name,
  4935.             "PRECISION" = convert(int,8),
  4936.             LENGTH = convert(int,8),
  4937.             SCALE = convert(smallint, NULL),
  4938.             PSEUDO_COLUMN = convert(smallint,1)
  4939.         FROM
  4940.             systypes t, syscolumns c
  4941.         WHERE
  4942.             c.id = @table_id
  4943.             AND c.usertype = 80 /*    TIMESTAMP */
  4944.             AND t.usertype = 80 /*    TIMESTAMP */
  4945.         RETURN
  4946.     END
  4947.  
  4948.     /* ROWID, now find the id of the 'best' index for this table */
  4949.  
  4950.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4951.                            nullable columns. */
  4952.  
  4953.         SELECT @indid = MIN(indid)
  4954.             FROM sysindexes i,syscolumns c,syscolumns c2
  4955.             WHERE
  4956.                 i.status&2 = 2        /*    If Unique Index */
  4957.                 AND c.id = i.id
  4958.                 AND c2.id = c.id
  4959.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4960.                 AND i.id = @table_id
  4961.                 AND indid > 0        /*    Eliminate Table Row */
  4962.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4963.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4964.  
  4965.     ELSE    /* Include indexes that are partially nullable. */
  4966.  
  4967.         SELECT @indid = MIN(indid)
  4968.             FROM sysindexes i
  4969.             WHERE
  4970.                 status&2 = 2        /*    If Unique Index */
  4971.                 AND id = @table_id
  4972.                 AND indid > 0        /*    Eliminate Table Row */
  4973.  
  4974.     SELECT
  4975.         SCOPE = @scopeout,
  4976.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4977.         d.DATA_TYPE,
  4978.         convert(varchar(32),case
  4979.             when (t.usertype > 100 or t.usertype in (18,80))
  4980.                 then t.name
  4981.             else d.TYPE_NAME
  4982.         end) TYPE_NAME,
  4983.         convert(int,case
  4984.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4985.             else isnull(convert(int,c.prec), 2147483647)
  4986.         end) "PRECISION",
  4987.         convert(int,case
  4988.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4989.                 convert(int,c.prec+2)
  4990.             else
  4991.                 isnull(d.length, c.length)
  4992.         end) LENGTH,
  4993.         SCALE = convert(smallint, c.scale),
  4994.         PSEUDO_COLUMN = convert(smallint,1)
  4995.     FROM
  4996.         sysindexes x,
  4997.         syscolumns c,
  4998.         master.dbo.spt_datatype_info d,
  4999.         systypes t,
  5000.         syscolumns c2    /* Self-join to generate list of index columns and */
  5001.                         /* to extract datatype names */
  5002.     WHERE
  5003.         x.id = @table_id
  5004.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5005.         AND c.id = x.id
  5006.         AND c2.id = x.id
  5007.         AND c2.colid < x.keycnt+(x.status&16)/16
  5008.         AND x.indid = @indid
  5009.         AND t.type = d.ss_dtype
  5010.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5011.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  5012.         AND c.usertype = t.usertype
  5013. go
  5014.  
  5015. if (charindex('7.00', @@version) = 0 and
  5016.     charindex('8.00', @@version) = 0)
  5017. begin
  5018.     print ''
  5019.     print ''
  5020.     print 'Warning:'
  5021.     print 'you are installing the stored procedures '
  5022.     print 'on a pre 7.0 SQL Server.'
  5023.     print 'Ignore the following errors.'
  5024. end
  5025. else
  5026.     drop proc sp_special_columns
  5027. go
  5028.  
  5029. /*    Procedure for 7.0 servers */
  5030. CREATE PROCEDURE sp_special_columns (
  5031.                  @table_name        sysname,
  5032.                  @table_owner        sysname = null,
  5033.                  @table_qualifier    sysname = null,
  5034.                  @col_type            char(1) = 'R',
  5035.                  @scope                char(1) = 'T',
  5036.                  @nullable            char(1) = 'U',
  5037.                  @ODBCVer            int = 2)
  5038. AS
  5039.     DECLARE @indid                int
  5040.     DECLARE @table_id            int
  5041.     DECLARE @full_table_name    nvarchar(257)
  5042.     DECLARE @scopeout            smallint
  5043.  
  5044.     if @col_type not in ('R','V') or @col_type is null
  5045.     begin
  5046.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5047.         return
  5048.     end
  5049.  
  5050.     if @scope = 'C'
  5051.         select @scopeout = 0
  5052.     else if @scope = 'T'
  5053.         select @scopeout = 1
  5054.     else
  5055.     begin
  5056.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5057.         return
  5058.     end
  5059.  
  5060.     if @nullable not in ('U','O') or @nullable is null
  5061.     begin
  5062.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5063.         return
  5064.     end
  5065.  
  5066.     if @table_qualifier is not null
  5067.     begin
  5068.         if db_name() <> @table_qualifier
  5069.         begin /* If qualifier doesn't match current database */
  5070.             raiserror (15250, -1,-1)
  5071.             return
  5072.         end
  5073.     end
  5074.     if @table_owner is null
  5075.     begin     /* If unqualified table name */
  5076.         SELECT @full_table_name = quotename(@table_name)
  5077.     end
  5078.     else
  5079.     begin    /* Qualified table name */
  5080.         if @table_owner = ''
  5081.         begin    /* If empty owner name */
  5082.             SELECT @full_table_name = quotename(@table_owner)
  5083.         end
  5084.         else
  5085.         begin
  5086.             SELECT @full_table_name = quotename(@table_owner) +
  5087.                 '.' + quotename(@table_name)
  5088.         end
  5089.     end
  5090.     /*    Get Object ID */
  5091.     SELECT @table_id = object_id(@full_table_name)
  5092.  
  5093.     if @col_type = 'V'
  5094.     BEGIN /* if ROWVER, just run that query */
  5095.         SELECT
  5096.             SCOPE = convert(smallint,NULL),
  5097.             COLUMN_NAME = convert(sysname,c.name),
  5098.             DATA_TYPE = convert(smallint, -2),
  5099.             TYPE_NAME = t.name,
  5100.             "PRECISION" = convert(int,8),
  5101.             LENGTH = convert(int,8),
  5102.             SCALE = convert(smallint, NULL),
  5103.             PSEUDO_COLUMN = convert(smallint,1)
  5104.         FROM
  5105.             systypes t, syscolumns c
  5106.         WHERE
  5107.             not (@table_id is null)
  5108.             AND c.id = @table_id
  5109.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  5110.             AND t.xtype = c.xtype        
  5111.             AND t.xusertype = c.xusertype    
  5112.         RETURN
  5113.     END
  5114.  
  5115.     /* ROWID, now find the id of the 'best' index for this table */
  5116.  
  5117.     IF @nullable = 'O'    /* Don't include any indexes that contain
  5118.                            nullable columns. */
  5119.  
  5120.         SELECT @indid = MIN(indid)
  5121.             FROM sysindexes x, syscolumns c, syscolumns c2
  5122.             WHERE
  5123.                 not (@table_id is null)
  5124.                 AND x.status&2 = 2        /*    If Unique Index */
  5125.                 AND c.id = x.id
  5126.                 AND c2.id = c.id
  5127.                 AND c2.colid < x.keycnt + (x.status&16)/16
  5128.                 AND x.id = @table_id
  5129.                 AND indid > 0        /*    Eliminate Table Row */
  5130.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5131.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5132.  
  5133.     ELSE    /* Include indexes that are partially nullable. */
  5134.  
  5135.         SELECT @indid = MIN(indid)
  5136.             FROM sysindexes x
  5137.             WHERE
  5138.                 not (@table_id is null)
  5139.                 AND status&2 = 2        /*    If Unique Index */
  5140.                 AND id = @table_id
  5141.                 AND indid > 0        /*    Eliminate Table Row */
  5142.  
  5143.     SELECT
  5144.         SCOPE = @scopeout,
  5145.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5146.         d.DATA_TYPE,
  5147.         convert(sysname,case
  5148.             when t.xusertype > 255 then t.name
  5149.             else d.TYPE_NAME
  5150.         end) TYPE_NAME,
  5151.         convert(int,case
  5152.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5153.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5154.         end) "PRECISION",
  5155.         convert(int,case
  5156.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5157.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5158.             else isnull(d.length, c.length)
  5159.         end) LENGTH,
  5160.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5161.         PSEUDO_COLUMN = convert(smallint,1)
  5162.     FROM
  5163.         sysindexes x,
  5164.         syscolumns c,
  5165.         master.dbo.spt_datatype_info d,
  5166.         systypes t,
  5167.         syscolumns c2    /* Self-join to generate list of index columns and */
  5168.                         /* to extract datatype names */
  5169.     WHERE
  5170.         not (@table_id is null)
  5171.         AND x.id = @table_id
  5172.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5173.         AND c.id = x.id
  5174.         AND c2.id = x.id
  5175.         AND c2.colid < x.keycnt + (x.status&16)/16
  5176.         AND x.indid = @indid
  5177.         AND t.xtype = d.ss_dtype
  5178.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5179.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5180.         AND c.xusertype = t.xusertype
  5181. go
  5182.  
  5183. if (charindex('8.00', @@version) = 0)
  5184. begin
  5185.     print ''
  5186.     print ''
  5187.     print 'Warning:'
  5188.     print 'you are installing the stored procedures '
  5189.     print 'on a pre 8.0 SQL Server.'
  5190.     print 'Ignore the following errors.'
  5191. end
  5192. else
  5193.     drop proc sp_special_columns
  5194. go
  5195.  
  5196. /*    Procedure for 8.0 and later servers */
  5197. CREATE PROCEDURE sp_special_columns (
  5198.                  @table_name        sysname,
  5199.                  @table_owner        sysname = null,
  5200.                  @table_qualifier    sysname = null,
  5201.                  @col_type            char(1) = 'R',
  5202.                  @scope                char(1) = 'T',
  5203.                  @nullable            char(1) = 'U',
  5204.                  @ODBCVer            int = 2)
  5205. AS
  5206.     DECLARE @indid                int
  5207.     DECLARE @table_id            int
  5208.     DECLARE @full_table_name    nvarchar(257)
  5209.     DECLARE @scopeout            smallint
  5210.  
  5211.     if @col_type not in ('R','V') or @col_type is null
  5212.     begin
  5213.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5214.         return
  5215.     end
  5216.  
  5217.     if @scope = 'C'
  5218.         select @scopeout = 0
  5219.     else if @scope = 'T'
  5220.         select @scopeout = 1
  5221.     else
  5222.     begin
  5223.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5224.         return
  5225.     end
  5226.  
  5227.     if @nullable not in ('U','O') or @nullable is null
  5228.     begin
  5229.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5230.         return
  5231.     end
  5232.  
  5233.     if @table_qualifier is not null
  5234.     begin
  5235.         if db_name() <> @table_qualifier
  5236.         begin /* If qualifier doesn't match current database */
  5237.             raiserror (15250, -1,-1)
  5238.             return
  5239.         end
  5240.     end
  5241.     if @table_owner is null
  5242.     begin     /* If unqualified table name */
  5243.         SELECT @full_table_name = quotename(@table_name)
  5244.     end
  5245.     else
  5246.     begin    /* Qualified table name */
  5247.         if @table_owner = ''
  5248.         begin    /* If empty owner name */
  5249.             SELECT @full_table_name = quotename(@table_owner)
  5250.         end
  5251.         else
  5252.         begin
  5253.             SELECT @full_table_name = quotename(@table_owner) +
  5254.                 '.' + quotename(@table_name)
  5255.         end
  5256.     end
  5257.     /*    Get Object ID */
  5258.     SELECT @table_id = object_id(@full_table_name)
  5259.  
  5260.     if @col_type = 'V'
  5261.     BEGIN /* if ROWVER, just run that query */
  5262.         SELECT
  5263.             SCOPE = convert(smallint,NULL),
  5264.             COLUMN_NAME = convert(sysname,c.name),
  5265.             DATA_TYPE = convert(smallint, -2),
  5266.             TYPE_NAME = t.name,
  5267.             "PRECISION" = convert(int,8),
  5268.             LENGTH = convert(int,8),
  5269.             SCALE = convert(smallint, NULL),
  5270.             PSEUDO_COLUMN = convert(smallint,1)
  5271.         FROM
  5272.             systypes t, syscolumns c
  5273.         WHERE
  5274.             not (@table_id is null)
  5275.             AND c.id = @table_id
  5276.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  5277.             AND t.xtype = c.xtype        
  5278.             AND t.xusertype = c.xusertype    
  5279.         RETURN
  5280.     END
  5281.  
  5282.     /* ROWID, now find the id of the 'best' index for this table */
  5283.  
  5284.     IF @nullable = 'O'    /* Don't include any indexes that contain
  5285.                            nullable columns. */
  5286.  
  5287.         SELECT @indid = MIN(indid)
  5288.             FROM sysindexes x, syscolumns c, syscolumns c2
  5289.             WHERE
  5290.                 not (@table_id is null)
  5291.                 AND x.status&2 = 2        /*    If Unique Index */
  5292.                 AND c.id = x.id
  5293.                 AND c2.id = c.id
  5294.                 AND c2.colid < x.keycnt + (x.status&18)/18
  5295.                 AND x.id = @table_id
  5296.                 AND indid > 0        /*    Eliminate Table Row */
  5297.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5298.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5299.  
  5300.     ELSE    /* Include indexes that are partially nullable. */
  5301.  
  5302.         SELECT @indid = MIN(indid)
  5303.             FROM sysindexes x
  5304.             WHERE
  5305.                 not (@table_id is null)
  5306.                 AND status&2 = 2        /*    If Unique Index */
  5307.                 AND id = @table_id
  5308.                 AND indid > 0        /*    Eliminate Table Row */
  5309.  
  5310.     SELECT
  5311.         SCOPE = @scopeout,
  5312.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5313.         d.DATA_TYPE,
  5314.         convert(sysname,case
  5315.             when t.xusertype > 255 then t.name
  5316.             else d.TYPE_NAME collate database_default
  5317.         end) TYPE_NAME,
  5318.         convert(int,case
  5319.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5320.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5321.         end) "PRECISION",
  5322.         convert(int,case
  5323.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5324.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5325.             else isnull(d.length, c.length)
  5326.         end) LENGTH,
  5327.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5328.         PSEUDO_COLUMN = convert(smallint,1)
  5329.     FROM
  5330.         sysindexes x,
  5331.         syscolumns c,
  5332.         master.dbo.spt_datatype_info d,
  5333.         systypes t,
  5334.         syscolumns c2    /* Self-join to generate list of index columns and */
  5335.                         /* to extract datatype names */
  5336.     WHERE
  5337.         not (@table_id is null)
  5338.         AND x.id = @table_id
  5339.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5340.         AND c.id = x.id
  5341.         AND c2.id = x.id
  5342.         AND c2.colid < x.keycnt + (x.status&16)/16
  5343.         AND x.indid = @indid
  5344.         AND t.xtype = d.ss_dtype
  5345.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5346.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5347.         AND c.xusertype = t.xusertype
  5348. go
  5349.  
  5350. grant execute on sp_special_columns to public
  5351. go
  5352.  
  5353. dump tran master with no_log
  5354. go
  5355.  
  5356. print 'creating sp_sproc_columns'
  5357. go
  5358.  
  5359. /*    Procedure for pre-6.0 server */
  5360. CREATE PROCEDURE sp_sproc_columns (
  5361.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  5362.                  @procedure_owner        varchar(96) = null,
  5363.                  @procedure_qualifier    varchar(32) = null,
  5364.                  @column_name            varchar(96) = null,
  5365.                  @ODBCVer                int = 2)
  5366. AS
  5367.     DECLARE @group_num_lower smallint
  5368.     DECLARE @group_num_upper smallint
  5369.     DECLARE @semi_position int
  5370.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  5371.     DECLARE @procedure_id int
  5372.  
  5373.     if @column_name is null /*    If column name not supplied, match all */
  5374.         select @column_name = '%'
  5375.     if @procedure_qualifier is not null
  5376.     begin
  5377.         if db_name() <> @procedure_qualifier
  5378.         begin
  5379.             if @procedure_qualifier = ''
  5380.             begin
  5381.                 /* in this case, we need to return an empty result set */
  5382.                 /* because the user has requested a database with an empty name */
  5383.                 select @procedure_name = ''
  5384.                 select @procedure_owner = ''
  5385.             end
  5386.             else
  5387.             begin    /* If qualifier doesn't match current database */
  5388.                 raiserror 20001 '~~Rush_51~~'
  5389.                 return
  5390.             end
  5391.         end
  5392.     end
  5393.  
  5394.     if @procedure_name is null
  5395.     begin    /*    If procedure name not supplied, match all */
  5396.         select @procedure_name = '%'
  5397.     end
  5398.  
  5399.     /* first we need to extract the procedure group number, if one exists */
  5400.     select @semi_position = charindex(';',@procedure_name)
  5401.     if (@semi_position > 0)
  5402.     begin    /* If group number separator (;) found */
  5403.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5404.         select @group_num_upper = @group_num_lower
  5405.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5406.     end
  5407.     else
  5408.     begin    /* No group separator, so default to group number of 1 */
  5409.         select @group_num_lower = 1
  5410.         select @group_num_upper = 32767            
  5411.     end
  5412.  
  5413.     if @procedure_owner is null
  5414.     begin    /* If unqualified procedure name */
  5415.         SELECT @full_procedure_name = @procedure_name
  5416.     end
  5417.     else
  5418.     begin    /* Qualified procedure name */
  5419.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5420.     end
  5421.  
  5422.     /*    Get Object ID */
  5423.     SELECT @procedure_id = object_id(@full_procedure_name)
  5424.     if ((charindex('%',@full_procedure_name) = 0) and
  5425.         (charindex('_',@full_procedure_name) = 0) and
  5426.         @procedure_id <> 0)
  5427.     begin
  5428.         /* this block is for the case where there is no pattern
  5429.             matching required for the procedure name */
  5430.         SELECT
  5431.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5432.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5433.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5434.             COLUMN_NAME = convert(varchar(32),c.name),
  5435.             COLUMN_TYPE = convert(smallint, 0),
  5436.             d.DATA_TYPE,
  5437.             TYPE_NAME = t.name,
  5438.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5439.             LENGTH = isnull(d.length, convert(int,c.length)),
  5440.             SCALE = d.numeric_scale,
  5441.             d.RADIX,
  5442.             d.NULLABLE,
  5443.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5444.             COLUMN_DEF = convert(varchar(255),null),
  5445.             d.SQL_DATA_TYPE,
  5446.             d.SQL_DATETIME_SUB,
  5447.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5448.             ORDINAL_POSITION = convert(int, c.colid),
  5449.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5450.             SS_DATA_TYPE = c.type
  5451.         FROM
  5452.             syscolumns c,
  5453.             sysobjects o,
  5454.             master.dbo.spt_datatype_info d,
  5455.             systypes t
  5456.         WHERE
  5457.             o.id = @procedure_id
  5458.             AND c.id = o.id
  5459.             AND t.type = d.ss_dtype
  5460.             AND c.length = isnull(d.fixlen, c.length)
  5461.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5462.             AND c.usertype = t.usertype
  5463.             AND c.name like @column_name
  5464.             AND c.number between @group_num_lower and @group_num_upper
  5465.         UNION ALL
  5466.         SELECT           /* return value row*/
  5467.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5468.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5469.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5470.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5471.             COLUMN_TYPE = convert(smallint, 5),
  5472.             DATA_TYPE = convert(smallint, 4),
  5473.             TYPE_NAME = convert(varchar(32),'int'),
  5474.             "PRECISION" = convert(int,10),
  5475.             LENGTH = convert(int,4),
  5476.             SCALE = convert(smallint,0),
  5477.             RADIX = convert(smallint,10),
  5478.             NULLABLE = convert(smallint,0),
  5479.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5480.             COLUMN_DEF = convert(varchar(255),NULL),
  5481.             SQL_DATA_TYPE = convert(smallint, 4),
  5482.             SQL_DATETIME_SUB = convert(smallint,null),
  5483.             CHAR_OCTET_LENGTH = convert(int,null),
  5484.             ORDINAL_POSITION = convert(int,0),
  5485.             IS_NULLABLE = convert(varchar(254),'NO'),
  5486.             SS_DATA_TYPE = convert(tinyint,56)
  5487.         FROM
  5488.             syscomments c, sysobjects o
  5489.         WHERE
  5490.             o.id = @procedure_id
  5491.             AND c.id = o.id
  5492.             AND c.colid = 1
  5493.             AND o.type = 'P'                        /* Just Procedures */
  5494.             AND '@RETURN_VALUE' like @column_name
  5495.             AND c.number between @group_num_lower and @group_num_upper        
  5496.         ORDER BY 1, 2, 3, 18
  5497.     end
  5498.     else
  5499.     begin
  5500.         /* this block is for the case where there IS pattern
  5501.             matching done on the procedure name */
  5502.         if @procedure_owner is null
  5503.             select @procedure_owner = '%'
  5504.         SELECT
  5505.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5506.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5507.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5508.             COLUMN_NAME = convert(varchar(32),c.name),
  5509.             COLUMN_TYPE = convert(smallint, 0),
  5510.             d.DATA_TYPE,
  5511.             TYPE_NAME = t.name,
  5512.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5513.             LENGTH = isnull(d.length, convert(int,c.length)),
  5514.             SCALE = d.numeric_scale,
  5515.             d.RADIX,
  5516.             d.NULLABLE,
  5517.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5518.             COLUMN_DEF = convert(varchar(255),null),
  5519.             d.SQL_DATA_TYPE,
  5520.             d.SQL_DATETIME_SUB,
  5521.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5522.             ORDINAL_POSITION = convert(int, c.colid),
  5523.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5524.             SS_DATA_TYPE = c.type
  5525.         FROM
  5526.             syscolumns c,
  5527.             sysobjects o,
  5528.             master.dbo.spt_datatype_info d,
  5529.             systypes t
  5530.         WHERE
  5531.             o.name like @procedure_name
  5532.             AND user_name(o.uid) like @procedure_owner
  5533.             AND o.id = c.id
  5534.             AND t.type = d.ss_dtype
  5535.             AND c.length = isnull(d.fixlen, c.length)
  5536.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5537.             AND c.usertype = t.usertype
  5538.             AND o.type = 'P'                            /* Just Procedures */
  5539.             AND c.name like @column_name
  5540.             AND c.number between @group_num_lower and @group_num_upper
  5541.         UNION ALL
  5542.         SELECT           /* return value row*/
  5543.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5544.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5545.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5546.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5547.             COLUMN_TYPE = convert(smallint, 5),
  5548.             DATA_TYPE = convert(smallint, 4),
  5549.             TYPE_NAME = convert(varchar(32),'int'),
  5550.             "PRECISION" = convert(int,10),
  5551.             LENGTH = convert(int,4),
  5552.             SCALE = convert(smallint,0),
  5553.             RADIX = convert(smallint,10),
  5554.             NULLABLE = convert(smallint,0),
  5555.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5556.             COLUMN_DEF = convert(varchar(255),NULL),
  5557.             SQL_DATA_TYPE = convert(smallint, 4),
  5558.             SQL_DATETIME_SUB = convert(smallint,null),
  5559.             CHAR_OCTET_LENGTH = convert(int,null),
  5560.             ORDINAL_POSITION = convert(int,0),
  5561.             IS_NULLABLE = convert(varchar(254),'NO'),
  5562.             SS_DATA_TYPE = convert(tinyint,56)
  5563.         FROM
  5564.             syscomments c, sysobjects o
  5565.         WHERE
  5566.             o.name like @procedure_name
  5567.             AND user_name(o.uid) like @procedure_owner
  5568.             AND c.id = o.id
  5569.             AND c.colid = 1
  5570.             AND o.type = 'P'                        /* Just Procedures */
  5571.             AND '@RETURN_VALUE' like @column_name
  5572.             AND c.number between @group_num_lower and @group_num_upper
  5573.         ORDER BY 1, 2, 3, 18
  5574.     end
  5575. go
  5576.  
  5577. if (charindex('6.00', @@version) = 0 and
  5578.     charindex('6.50', @@version) = 0 and
  5579.     charindex('7.00', @@version) = 0 and
  5580.     charindex('8.00', @@version) = 0)
  5581. begin
  5582.     print ''
  5583.     print ''
  5584.     print 'Warning:'
  5585.     print 'you are installing the stored procedures '
  5586.     print 'on a pre 6.0 SQL Server.'
  5587.     print 'Ignore the following error.'
  5588. end
  5589. else
  5590.     drop proc sp_sproc_columns
  5591. go
  5592.  
  5593. /*    Procedure for 6.0 and 6.50 servers */
  5594. CREATE PROCEDURE sp_sproc_columns (
  5595.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  5596.                  @procedure_owner        varchar(96) = null,
  5597.                  @procedure_qualifier    varchar(32) = null,
  5598.                  @column_name            varchar(96) = null,
  5599.                  @ODBCVer                int = 2)
  5600. AS
  5601.     DECLARE @group_num_lower smallint
  5602.     DECLARE @group_num_upper smallint
  5603.     DECLARE @semi_position int
  5604.     DECLARE @full_procedure_name    varchar(205)
  5605.     DECLARE @procedure_id int
  5606.  
  5607.     if @column_name is null /*    If column name not supplied, match all */
  5608.         select @column_name = '%'
  5609.     if @procedure_qualifier is not null
  5610.     begin
  5611.         if db_name() <> @procedure_qualifier
  5612.         begin
  5613.             if @procedure_qualifier = ''
  5614.             begin
  5615.                 /* in this case, we need to return an empty result set */
  5616.                 /* because the user has requested a database with an empty name */
  5617.                 select @procedure_name = ''
  5618.                 select @procedure_owner = ''
  5619.             end
  5620.             else
  5621.             begin    /* If qualifier doesn't match current database */
  5622.                 raiserror (15250, -1,-1)
  5623.                 return
  5624.             end
  5625.         end
  5626.     end
  5627.  
  5628.     if @procedure_name is null
  5629.     begin    /*    If procedure name not supplied, match all */
  5630.         select @procedure_name = '%'
  5631.     end
  5632.  
  5633.     /* first we need to extract the procedure group number, if one exists */
  5634.     select @semi_position = charindex(';',@procedure_name)
  5635.     if (@semi_position > 0)
  5636.     begin    /* If group number separator (;) found */
  5637.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5638.         select @group_num_upper = @group_num_lower
  5639.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5640.     end
  5641.     else
  5642.     begin    /* No group separator, so default to group number of 1 */
  5643.         select @group_num_lower = 1
  5644.         select @group_num_upper = 32767            
  5645.     end
  5646.  
  5647.     if @procedure_owner is null
  5648.     begin    /* If unqualified procedure name */
  5649.         SELECT @full_procedure_name = @procedure_name
  5650.     end
  5651.     else
  5652.     begin    /* Qualified procedure name */
  5653.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5654.     end
  5655.  
  5656.     /*    Get Object ID */
  5657.     SELECT @procedure_id = object_id(@full_procedure_name)
  5658.     if ((charindex('%',@full_procedure_name) = 0) and
  5659.         (charindex('[',@full_procedure_name) = 0) and
  5660.         (charindex('_',@full_procedure_name) = 0) and
  5661.         @procedure_id <> 0)
  5662.     begin
  5663.         /* this block is for the case where there is no pattern
  5664.             matching required for the procedure name */
  5665.         SELECT
  5666.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5667.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5668.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5669.             COLUMN_NAME = convert(varchar(32),c.name),
  5670.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5671.             d.DATA_TYPE,
  5672.             TYPE_NAME = t.name,
  5673.             convert(int,case
  5674.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5675.                 else isnull(convert(int,c.prec), 2147483647)
  5676.             end) "PRECISION",
  5677.             convert(int,case
  5678.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5679.                     c.prec+2
  5680.                 else
  5681.                     isnull(d.length, c.length)
  5682.             end) LENGTH,
  5683.             SCALE = convert(smallint, c.scale),
  5684.             d.RADIX,
  5685.             d.NULLABLE,
  5686.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5687.             COLUMN_DEF = convert(varchar(255),NULL),
  5688.             d.SQL_DATA_TYPE,
  5689.             d.SQL_DATETIME_SUB,
  5690.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5691.             ORDINAL_POSITION = convert(int, c.colid),
  5692.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5693.             SS_DATA_TYPE = c.type
  5694.         FROM
  5695.             syscolumns c,
  5696.             sysobjects o,
  5697.             master.dbo.spt_datatype_info d,
  5698.             systypes t
  5699.         WHERE
  5700.             o.id = @procedure_id
  5701.             AND c.id = o.id
  5702.             AND c.type = d.ss_dtype
  5703.             AND c.length = isnull(d.fixlen, c.length)
  5704.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5705.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5706.             AND c.usertype = t.usertype
  5707.             AND c.name like @column_name
  5708.             AND c.number between @group_num_lower and @group_num_upper
  5709.         UNION ALL
  5710.         SELECT           /* return value row*/
  5711.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5712.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5713.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5714.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5715.             COLUMN_TYPE = convert(smallint, 5),
  5716.             DATA_TYPE = convert(smallint, 4),
  5717.             TYPE_NAME = convert(varchar(32),'int'),
  5718.             "PRECISION" = convert(int,10),
  5719.             LENGTH = convert(int,4),
  5720.             SCALE = convert(smallint,0),
  5721.             RADIX = convert(smallint,10),
  5722.             NULLABLE = convert(smallint,0),
  5723.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5724.             COLUMN_DEF = convert(varchar(255),NULL),
  5725.             SQL_DATA_TYPE = convert(smallint, 4),
  5726.             SQL_DATETIME_SUB = convert(smallint,null),
  5727.             CHAR_OCTET_LENGTH = convert(int,null),
  5728.             ORDINAL_POSITION = convert(int,0),
  5729.             IS_NULLABLE = convert(varchar(254),'NO'),
  5730.             SS_DATA_TYPE = convert(tinyint,56)
  5731.         FROM
  5732.             syscomments c, sysobjects o
  5733.         WHERE
  5734.             o.id = @procedure_id
  5735.             AND c.id = o.id
  5736.             AND c.colid = 1
  5737.             AND o.type = 'P'                        /* Just Procedures */
  5738.             AND '@RETURN_VALUE' like @column_name
  5739.             AND c.number between @group_num_lower and @group_num_upper
  5740.         ORDER BY 1, 2, 3, 18
  5741.     end
  5742.     else
  5743.     begin
  5744.         /* this block is for the case where there IS pattern
  5745.             matching done on the procedure name */
  5746.         if @procedure_owner is null
  5747.             select @procedure_owner = '%'
  5748.         SELECT
  5749.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5750.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5751.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5752.             COLUMN_NAME = convert(varchar(32),c.name),
  5753.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5754.             d.DATA_TYPE,
  5755.             TYPE_NAME = t.name,
  5756.             convert(int,case
  5757.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5758.                 else isnull(convert(int,c.prec), 2147483647)
  5759.             end) "PRECISION",
  5760.             convert(int,case
  5761.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5762.                     c.prec+2
  5763.                 else
  5764.                     isnull(d.length, c.length)
  5765.             end) LENGTH,
  5766.             SCALE = convert(smallint, c.scale),
  5767.             d.RADIX,
  5768.             d.NULLABLE,
  5769.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5770.             COLUMN_DEF = convert(varchar(255),NULL),
  5771.             d.SQL_DATA_TYPE,
  5772.             d.SQL_DATETIME_SUB,
  5773.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5774.             ORDINAL_POSITION = convert(int, c.colid),
  5775.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5776.             SS_DATA_TYPE = c.type
  5777.         FROM
  5778.             syscolumns c,
  5779.             sysobjects o,
  5780.             master.dbo.spt_datatype_info d,
  5781.             systypes t
  5782.         WHERE
  5783.             o.name like @procedure_name
  5784.             AND user_name(o.uid) like @procedure_owner
  5785.             AND o.id = c.id
  5786.             AND c.type = d.ss_dtype
  5787.             AND c.length = isnull(d.fixlen, c.length)
  5788.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5789.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5790.             AND c.usertype = t.usertype
  5791.             AND o.type = 'P'                            /* Just Procedures */
  5792.             AND c.name like @column_name
  5793.             AND c.number between @group_num_lower and @group_num_upper
  5794.         UNION ALL
  5795.         SELECT           /* return value row*/
  5796.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5797.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5798.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5799.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5800.             COLUMN_TYPE = convert(smallint, 5),
  5801.             DATA_TYPE = convert(smallint, 4),
  5802.             TYPE_NAME = convert(varchar(32),'int'),
  5803.             "PRECISION" = convert(int,10),
  5804.             LENGTH = convert(int,4),
  5805.             SCALE = convert(smallint,0),
  5806.             RADIX = convert(smallint,10),
  5807.             NULLABLE = convert(smallint,0),
  5808.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5809.             COLUMN_DEF = convert(varchar(255),NULL),
  5810.             SQL_DATA_TYPE = convert(smallint, 4),
  5811.             SQL_DATETIME_SUB = convert(smallint,null),
  5812.             CHAR_OCTET_LENGTH = convert(int,null),
  5813.             ORDINAL_POSITION = convert(int,0),
  5814.             IS_NULLABLE = convert(varchar(254),'NO'),
  5815.             SS_DATA_TYPE = convert(tinyint,56)
  5816.         FROM
  5817.             syscomments c, sysobjects o
  5818.         WHERE
  5819.             o.name like @procedure_name
  5820.             AND user_name(o.uid) like @procedure_owner
  5821.             AND c.id = o.id
  5822.             AND c.colid = 1
  5823.             AND o.type = 'P'                        /* Just Procedures */
  5824.             AND '@RETURN_VALUE' like @column_name
  5825.             AND c.number between @group_num_lower and @group_num_upper
  5826.         ORDER BY 1, 2, 3, 18
  5827.     end
  5828. go
  5829.  
  5830. if (charindex('7.00', @@version) = 0 and
  5831.     charindex('8.00', @@version) = 0)
  5832. begin
  5833.     print ''
  5834.     print ''
  5835.     print 'Warning:'
  5836.     print 'you are installing the stored procedures '
  5837.     print 'on a pre 7.0 SQL Server.'
  5838.     print 'Ignore the following errors.'
  5839. end
  5840. else
  5841.     drop proc sp_sproc_columns
  5842. go
  5843.  
  5844. /*    Procedure for 7.0 server */
  5845. CREATE PROCEDURE sp_sproc_columns (
  5846.                  @procedure_name        nvarchar(390) = '%',
  5847.                  @procedure_owner        nvarchar(384) = null,
  5848.                  @procedure_qualifier    sysname = null,
  5849.                  @column_name            nvarchar(384) = null,
  5850.                  @ODBCVer                int = 2)
  5851. AS
  5852.     DECLARE @group_num_lower smallint
  5853.     DECLARE @group_num_upper smallint
  5854.     DECLARE @semi_position int
  5855.     DECLARE @full_procedure_name    nvarchar(774)
  5856.     DECLARE @procedure_id int
  5857.  
  5858.     if @column_name is null /*    If column name not supplied, match all */
  5859.         select @column_name = '%'
  5860.     if @procedure_qualifier is not null
  5861.     begin
  5862.         if db_name() <> @procedure_qualifier
  5863.         begin
  5864.             if @procedure_qualifier = ''
  5865.             begin
  5866.                 /* in this case, we need to return an empty result set */
  5867.                 /* because the user has requested a database with an empty name */
  5868.                 select @procedure_name = ''
  5869.                 select @procedure_owner = ''
  5870.             end
  5871.             else
  5872.             begin    /* If qualifier doesn't match current database */
  5873.                 raiserror (15250, -1,-1)
  5874.                 return
  5875.             end
  5876.         end
  5877.     end
  5878.  
  5879.     if @procedure_name is null
  5880.     begin    /*    If procedure name not supplied, match all */
  5881.         select @procedure_name = '%'
  5882.     end
  5883.  
  5884.     /* first we need to extract the procedure group number, if one exists */
  5885.     select @semi_position = charindex(';',@procedure_name)
  5886.     if (@semi_position > 0)
  5887.     begin    /* If group number separator (;) found */
  5888.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5889.         select @group_num_upper = @group_num_lower
  5890.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5891.     end
  5892.     else
  5893.     begin    /* No group separator, so default to all groups */
  5894.         select @group_num_lower = 1
  5895.         select @group_num_upper = 32767            
  5896.     end
  5897.  
  5898.     if @procedure_owner is null
  5899.     begin    /* If unqualified procedure name */
  5900.         SELECT @full_procedure_name = quotename(@procedure_name)
  5901.     end
  5902.     else
  5903.     begin    /* Qualified procedure name */
  5904.         if @procedure_owner = ''
  5905.         begin    /* If empty owner name */
  5906.             SELECT @full_procedure_name = quotename(@procedure_owner)
  5907.         end
  5908.         else
  5909.         begin
  5910.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  5911.                 '.' + quotename(@procedure_name)
  5912.         end
  5913.     end
  5914.  
  5915.     /*    Get Object ID */
  5916.     SELECT @procedure_id = object_id(@full_procedure_name)
  5917.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  5918.         (isnull(charindex('[', @procedure_name),0) = 0) and
  5919.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  5920.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  5921.         not (@procedure_id is null))
  5922.     begin
  5923.         /* this block is for the case where there is no pattern
  5924.             matching required for the procedure name */
  5925.         SELECT
  5926.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5927.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5928.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5929.             COLUMN_NAME = convert(sysname,c.name),
  5930.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  5931.             d.DATA_TYPE,
  5932.             TYPE_NAME = t.name,
  5933.             convert(int,case
  5934.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5935.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  5936.             end) "PRECISION",
  5937.             convert(int,case
  5938.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5939.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  5940.                 else
  5941.                     isnull(d.length, c.length)
  5942.             end) LENGTH,
  5943.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5944.             d.RADIX,
  5945.             d.NULLABLE,
  5946.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5947.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5948.             d.SQL_DATA_TYPE,
  5949.             d.SQL_DATETIME_SUB,
  5950.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  5951.             ORDINAL_POSITION = convert(int, c.colid),
  5952.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5953.             SS_DATA_TYPE = c.type
  5954.         FROM
  5955.             syscolumns c,
  5956.             sysobjects o,
  5957.             master.dbo.spt_datatype_info d,
  5958.             systypes t
  5959.         WHERE
  5960.             o.id = @procedure_id
  5961.             AND c.id = o.id
  5962.             AND c.xtype = d.ss_dtype
  5963.             AND c.length = isnull(d.fixlen, c.length)
  5964.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5965.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5966.             AND c.xusertype = t.xusertype
  5967.             AND c.name like @column_name
  5968.             AND c.number between @group_num_lower and @group_num_upper
  5969.         UNION ALL
  5970.         SELECT           /* return value row*/
  5971.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5972.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5973.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5974.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  5975.             COLUMN_TYPE = convert(smallint, 5),
  5976.             DATA_TYPE = convert(smallint, 4),
  5977.             TYPE_NAME = convert(sysname,'int'),
  5978.             "PRECISION" = convert(int,10),
  5979.             LENGTH = convert(int,4),
  5980.             SCALE = convert(smallint,0),
  5981.             RADIX = convert(smallint,10),
  5982.             NULLABLE = convert(smallint,0),
  5983.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5984.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5985.             SQL_DATA_TYPE = convert(smallint, 4),
  5986.             SQL_DATETIME_SUB = convert(smallint,null),
  5987.             CHAR_OCTET_LENGTH = convert(int,null),
  5988.             ORDINAL_POSITION = convert(int,0),
  5989.             IS_NULLABLE = convert(varchar(254),'NO'),
  5990.             SS_DATA_TYPE = convert(tinyint,56)
  5991.         FROM
  5992.             syscomments c, sysobjects o
  5993.         WHERE
  5994.             o.id = @procedure_id
  5995.             AND c.id = o.id
  5996.             AND c.colid = 1
  5997.             AND o.type = 'P'                        /* Just Procedures */
  5998.             AND '@RETURN_VALUE' like @column_name
  5999.             AND c.number between @group_num_lower and @group_num_upper        
  6000.         ORDER BY 1, 2, 3, 18
  6001.     end
  6002.     else
  6003.     begin
  6004.         /* this block is for the case where there IS pattern
  6005.             matching done on the procedure name */
  6006.         if @procedure_owner is null
  6007.             select @procedure_owner = '%'
  6008.         SELECT
  6009.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6010.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6011.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6012.             COLUMN_NAME = convert(sysname,c.name),
  6013.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  6014.             d.DATA_TYPE,
  6015.             TYPE_NAME = t.name,
  6016.             convert(int,case
  6017.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6018.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6019.             end) "PRECISION",
  6020.             convert(int,case
  6021.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6022.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6023.                 else
  6024.                     isnull(d.length, c.length)
  6025.             end) LENGTH,
  6026.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6027.             d.RADIX,
  6028.             d.NULLABLE,
  6029.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6030.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6031.             d.SQL_DATA_TYPE,
  6032.             d.SQL_DATETIME_SUB,
  6033.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6034.             ORDINAL_POSITION = convert(int, c.colid),
  6035.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6036.             SS_DATA_TYPE = c.type
  6037.         FROM
  6038.             syscolumns c,
  6039.             sysobjects o,
  6040.             master.dbo.spt_datatype_info d,
  6041.             systypes t
  6042.         WHERE
  6043.             o.name like @procedure_name
  6044.             AND user_name(o.uid) like @procedure_owner
  6045.             AND o.id = c.id
  6046.             AND c.xtype = d.ss_dtype
  6047.             AND c.length = isnull(d.fixlen, c.length)
  6048.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6049.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6050.             AND c.xusertype = t.xusertype
  6051.             AND o.type = 'P'                            /* Just Procedures */
  6052.             AND c.name like @column_name
  6053.             AND c.number between @group_num_lower and @group_num_upper
  6054.         UNION ALL
  6055.         SELECT           /* return value row*/
  6056.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6057.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6058.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6059.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6060.             COLUMN_TYPE = convert(smallint, 5),
  6061.             DATA_TYPE = convert(smallint, 4),
  6062.             TYPE_NAME = convert(sysname,'int'),
  6063.             "PRECISION" = convert(int,10),
  6064.             LENGTH = convert(int,4),
  6065.             SCALE = convert(smallint,0),
  6066.             RADIX = convert(smallint,10),
  6067.             NULLABLE = convert(smallint,0),
  6068.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6069.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6070.             SQL_DATA_TYPE = convert(smallint, 4),
  6071.             SQL_DATETIME_SUB = convert(smallint,null),
  6072.             CHAR_OCTET_LENGTH = convert(int,null),
  6073.             ORDINAL_POSITION = convert(int,0),
  6074.             IS_NULLABLE = convert(varchar(254),'NO'),
  6075.             SS_DATA_TYPE = convert(tinyint,56)
  6076.         FROM
  6077.             syscomments c, sysobjects o
  6078.         WHERE
  6079.             o.name like @procedure_name
  6080.             AND c.id = o.id
  6081.             AND user_name(o.uid) like @procedure_owner
  6082.             AND c.colid = 1
  6083.             AND o.type = 'P'                        /* Just Procedures */
  6084.             AND '@RETURN_VALUE' like @column_name
  6085.             AND c.number between @group_num_lower and @group_num_upper
  6086.         ORDER BY 1, 2, 3, 18
  6087.     end
  6088. go
  6089.  
  6090. if (charindex('8.00', @@version) = 0)
  6091. begin
  6092.     print ''
  6093.     print ''
  6094.     print 'Warning:'
  6095.     print 'you are installing the stored procedures '
  6096.     print 'on a pre 8.0 SQL Server.'
  6097.     print 'Ignore the following errors.'
  6098. end
  6099. else
  6100.     drop proc sp_sproc_columns
  6101. go
  6102.  
  6103. /*    Procedure for 8.0 server */
  6104. CREATE PROCEDURE sp_sproc_columns (
  6105.                  @procedure_name        nvarchar(390) = '%',
  6106.                  @procedure_owner        nvarchar(384) = null,
  6107.                  @procedure_qualifier        sysname = null,
  6108.                  @column_name            nvarchar(384) = null,
  6109.                  @ODBCVer                int = 2)
  6110. AS
  6111.     DECLARE @group_num_lower smallint
  6112.     DECLARE @group_num_upper smallint
  6113.     DECLARE @semi_position int
  6114.     DECLARE @full_procedure_name nvarchar(774)
  6115.     DECLARE @procedure_id int
  6116.  
  6117.     if @column_name is null /*    If column name not supplied, match all */
  6118.         select @column_name = '%'
  6119.     if @procedure_qualifier is not null
  6120.     begin
  6121.         if db_name() <> @procedure_qualifier
  6122.         begin
  6123.             if @procedure_qualifier = ''
  6124.             begin
  6125.                 /* in this case, we need to return an empty result set */
  6126.                 /* because the user has requested a database with an empty name */
  6127.                 select @procedure_name = ''
  6128.                 select @procedure_owner = ''
  6129.             end
  6130.             else
  6131.             begin    /* If qualifier doesn't match current database */
  6132.                 raiserror (15250, -1,-1)
  6133.                 return
  6134.             end
  6135.         end
  6136.     end
  6137.  
  6138.     if @procedure_name is null
  6139.     begin    /*    If procedure name not supplied, match all */
  6140.         select @procedure_name = '%'
  6141.     end
  6142.  
  6143.     /* first we need to extract the procedure group number, if one exists */
  6144.     select @semi_position = charindex(';',@procedure_name)
  6145.     if (@semi_position > 0)
  6146.     begin    /* If group number separator (;) found */
  6147.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  6148.         select @group_num_upper = @group_num_lower
  6149.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  6150.     end
  6151.     else
  6152.     begin    /* No group separator, so default to all groups */
  6153.         select @group_num_lower = 1
  6154.         select @group_num_upper = 32767            
  6155.     end
  6156.  
  6157.     if @procedure_owner is null
  6158.     begin    /* If unqualified procedure name */
  6159.         SELECT @full_procedure_name = quotename(@procedure_name)
  6160.     end
  6161.     else
  6162.     begin    /* Qualified procedure name */
  6163.         if @procedure_owner = ''
  6164.         begin    /* If empty owner name */
  6165.             SELECT @full_procedure_name = quotename(@procedure_owner)
  6166.         end
  6167.         else
  6168.         begin
  6169.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  6170.                 '.' + quotename(@procedure_name)
  6171.         end
  6172.     end
  6173.  
  6174.     /*    Get Object ID */
  6175.     SELECT @procedure_id = object_id(@full_procedure_name)
  6176.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  6177.         (isnull(charindex('[', @procedure_name),0) = 0) and
  6178.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  6179.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  6180.         not (@procedure_id is null))
  6181.     begin
  6182.         /* this block is for the case where there is no pattern
  6183.             matching required for the procedure name */
  6184.         SELECT
  6185.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6186.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6187.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6188.             COLUMN_NAME = convert(sysname,c.name),
  6189.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  6190.             d.DATA_TYPE,
  6191.             TYPE_NAME = t.name,
  6192.             convert(int,case
  6193.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6194.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6195.             end) "PRECISION",
  6196.             convert(int,case
  6197.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6198.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6199.                 else
  6200.                     isnull(d.length, c.length)
  6201.             end) LENGTH,
  6202.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6203.             d.RADIX,
  6204.             d.NULLABLE,
  6205.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6206.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6207.             d.SQL_DATA_TYPE,
  6208.             d.SQL_DATETIME_SUB,
  6209.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6210.             ORDINAL_POSITION = convert(int, c.colid),
  6211.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6212.             SS_DATA_TYPE = c.type
  6213.         FROM
  6214.             syscolumns c,
  6215.             sysobjects o,
  6216.             master.dbo.spt_datatype_info d,
  6217.             systypes t
  6218.         WHERE
  6219.             o.id = @procedure_id
  6220.             AND c.id = o.id
  6221.             AND c.xtype = d.ss_dtype
  6222.             AND c.length = isnull(d.fixlen, c.length)
  6223.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6224.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6225.             AND c.xusertype = t.xusertype
  6226.             AND c.name like @column_name
  6227.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6228.             AND ((c.number between @group_num_lower and @group_num_upper)
  6229.                  OR (c.number = 0 and o.type = 'FN'))
  6230.         UNION ALL
  6231.         SELECT           /* return value row*/
  6232.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6233.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6234.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6235.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6236.             COLUMN_TYPE = convert(smallint, 5),
  6237.             DATA_TYPE = convert(smallint, 4),
  6238.             TYPE_NAME = convert(sysname,'int'),
  6239.             "PRECISION" = convert(int,10),
  6240.             LENGTH = convert(int,4),
  6241.             SCALE = convert(smallint,0),
  6242.             RADIX = convert(smallint,10),
  6243.             NULLABLE = convert(smallint,0),
  6244.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6245.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6246.             SQL_DATA_TYPE = convert(smallint, 4),
  6247.             SQL_DATETIME_SUB = convert(smallint,null),
  6248.             CHAR_OCTET_LENGTH = convert(int,null),
  6249.             ORDINAL_POSITION = convert(int,0),
  6250.             IS_NULLABLE = convert(varchar(254),'NO'),
  6251.             SS_DATA_TYPE = convert(tinyint,56)
  6252.         FROM
  6253.             syscomments c, sysobjects o
  6254.         WHERE
  6255.             o.id = @procedure_id
  6256.             AND c.id = o.id
  6257.             AND c.colid = 1
  6258.             AND o.type = 'P'            /* Procedures */
  6259.             AND '@RETURN_VALUE' like @column_name
  6260.             AND c.number between @group_num_lower and @group_num_upper
  6261.         UNION ALL
  6262.         SELECT        /* UDF return value */
  6263.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6264.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6265.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6266.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6267.             COLUMN_TYPE = convert(smallint, 5),    /* SQL_RETURN_VALUE */
  6268.             d.DATA_TYPE,
  6269.             TYPE_NAME = t.name,
  6270.             convert(int,case
  6271.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6272.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6273.             end) "PRECISION",
  6274.             convert(int,case
  6275.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6276.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6277.                 else
  6278.                     isnull(d.length, c.length)
  6279.             end) LENGTH,
  6280.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6281.             d.RADIX,
  6282.             NULLABLE = convert(smallint, c.isnullable),
  6283.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6284.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6285.             d.SQL_DATA_TYPE,
  6286.             d.SQL_DATETIME_SUB,
  6287.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6288.             ORDINAL_POSITION = convert(int,0),
  6289.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6290.             SS_DATA_TYPE = c.type
  6291.         FROM
  6292.             syscolumns c,
  6293.             sysobjects o,
  6294.             master.dbo.spt_datatype_info d,
  6295.             systypes t
  6296.         WHERE
  6297.             o.name like @procedure_name
  6298.             AND o.id = c.id
  6299.             AND c.xtype = d.ss_dtype
  6300.             AND c.length = isnull(d.fixlen, c.length)
  6301.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6302.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6303.             AND c.xusertype = t.xusertype
  6304.             AND o.type = 'FN'            /* Scalar UDF */
  6305.             AND c.name like @column_name
  6306.             AND c.colid = 0
  6307.             AND c.number = 0
  6308.         UNION ALL
  6309.         SELECT        /* Table valued functions */
  6310.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6311.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6312.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6313.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6314.             COLUMN_TYPE = convert(smallint, 3),
  6315.             DATA_TYPE = convert(smallint, NULL),
  6316.             TYPE_NAME = convert(sysname, 'table'),
  6317.             "PRECISION" = convert(int,0),
  6318.             LENGTH = convert(int,0),
  6319.             SCALE = convert(smallint,0),
  6320.             RADIX = convert(smallint,0),
  6321.             NULLABLE = convert(smallint,0),
  6322.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6323.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6324.             SQL_DATA_TYPE = convert(smallint, NULL),
  6325.             SQL_DATETIME_SUB = convert(smallint,null),
  6326.             CHAR_OCTET_LENGTH = convert(int,null),
  6327.             ORDINAL_POSITION = convert(int,0),
  6328.             IS_NULLABLE = convert(varchar(254),'NO'),
  6329.             SS_DATA_TYPE = convert(tinyint,0)
  6330.         FROM
  6331.             syscomments c, sysobjects o
  6332.         WHERE
  6333.             o.id = @procedure_id
  6334.             AND c.id = o.id
  6335.             AND c.colid = 1
  6336.             AND o.type IN ('TF', 'IF')
  6337.             AND '@TABLE_RETURN_VALUE' like @column_name
  6338.             AND c.number = 0
  6339.         ORDER BY 1, 2, 3, 18
  6340.     end
  6341.     else
  6342.     begin
  6343.         /* this block is for the case where there IS pattern
  6344.             matching done on the procedure name */
  6345.         if @procedure_owner is null
  6346.             select @procedure_owner = '%'
  6347.  
  6348.         SELECT
  6349.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6350.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6351.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6352.             COLUMN_NAME = convert(sysname,c.name),
  6353.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  6354.             d.DATA_TYPE,
  6355.             TYPE_NAME = t.name,
  6356.             convert(int,case
  6357.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6358.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6359.             end) "PRECISION",
  6360.             convert(int,case
  6361.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6362.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6363.                 else
  6364.                     isnull(d.length, c.length)
  6365.             end) LENGTH,
  6366.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6367.             d.RADIX,
  6368.             d.NULLABLE,
  6369.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6370.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6371.             d.SQL_DATA_TYPE,
  6372.             d.SQL_DATETIME_SUB,
  6373.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6374.             ORDINAL_POSITION = convert(int, c.colid),
  6375.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6376.             SS_DATA_TYPE = c.type
  6377.         FROM
  6378.             syscolumns c,
  6379.             sysobjects o,
  6380.             master.dbo.spt_datatype_info d,
  6381.             systypes t
  6382.         WHERE
  6383.             o.name like @procedure_name
  6384.             AND user_name(o.uid) like @procedure_owner
  6385.             AND o.id = c.id
  6386.             AND c.xtype = d.ss_dtype
  6387.             AND c.length = isnull(d.fixlen, c.length)
  6388.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6389.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6390.             AND c.xusertype = t.xusertype
  6391.             AND c.name like @column_name
  6392.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6393.             AND ((c.number between @group_num_lower and @group_num_upper)
  6394.                  OR (c.number = 0 and o.type = 'FN'))
  6395.         UNION ALL
  6396.         SELECT           /* return value row*/
  6397.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6398.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6399.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6400.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6401.             COLUMN_TYPE = convert(smallint, 5),    /* SQL_RETURN_VALUE */
  6402.             DATA_TYPE = convert(smallint, 4),
  6403.             TYPE_NAME = convert(sysname,'int'),
  6404.             "PRECISION" = convert(int,10),
  6405.             LENGTH = convert(int,4),
  6406.             SCALE = convert(smallint,0),
  6407.             RADIX = convert(smallint,10),
  6408.             NULLABLE = convert(smallint,0),
  6409.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6410.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6411.             SQL_DATA_TYPE = convert(smallint, 4),
  6412.             SQL_DATETIME_SUB = convert(smallint,null),
  6413.             CHAR_OCTET_LENGTH = convert(int,null),
  6414.             ORDINAL_POSITION = convert(int,0),
  6415.             IS_NULLABLE = convert(varchar(254),'NO'),
  6416.             SS_DATA_TYPE = convert(tinyint,56)
  6417.         FROM
  6418.             syscomments c, sysobjects o
  6419.         WHERE
  6420.             o.name like @procedure_name
  6421.             AND c.id = o.id
  6422.             AND user_name(o.uid) like @procedure_owner
  6423.             AND c.colid = 1
  6424.             AND o.type = 'P'                    /* Procedures */
  6425.             AND '@RETURN_VALUE' like @column_name
  6426.             AND c.number between @group_num_lower and @group_num_upper
  6427.         UNION ALL
  6428.         SELECT        /* UDF return value */
  6429.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6430.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6431.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6432.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6433.             COLUMN_TYPE = convert(smallint, 5),    /* SQL_RETURN_VALUE */
  6434.             d.DATA_TYPE,
  6435.             TYPE_NAME = t.name,
  6436.             convert(int,case
  6437.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6438.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6439.             end) "PRECISION",
  6440.             convert(int,case
  6441.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6442.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6443.                 else
  6444.                     isnull(d.length, c.length)
  6445.             end) LENGTH,
  6446.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6447.             d.RADIX,
  6448.             NULLABLE = convert(smallint, c.isnullable),
  6449.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6450.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6451.             d.SQL_DATA_TYPE,
  6452.             d.SQL_DATETIME_SUB,
  6453.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6454.             ORDINAL_POSITION = convert(int,0),
  6455.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6456.             SS_DATA_TYPE = c.type
  6457.         FROM
  6458.             syscolumns c,
  6459.             sysobjects o,
  6460.             master.dbo.spt_datatype_info d,
  6461.             systypes t
  6462.         WHERE
  6463.             o.name like @procedure_name
  6464.             AND user_name(o.uid) like @procedure_owner
  6465.             AND o.id = c.id
  6466.             AND c.xtype = d.ss_dtype
  6467.             AND c.length = isnull(d.fixlen, c.length)
  6468.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6469.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6470.             AND c.xusertype = t.xusertype
  6471.             AND o.type = 'FN'            /* Scalar UDF */
  6472.             AND c.name like @column_name
  6473.             AND c.colid = 0
  6474.             AND c.number = 0
  6475.         UNION ALL
  6476.         SELECT        /* Table valued functions */
  6477.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6478.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6479.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6480.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6481.             COLUMN_TYPE = convert(smallint, 3),
  6482.             DATA_TYPE = convert(smallint, NULL),
  6483.             TYPE_NAME = convert(sysname, 'table'),
  6484.             "PRECISION" = convert(int,0),
  6485.             LENGTH = convert(int,0),
  6486.             SCALE = convert(smallint,0),
  6487.             RADIX = convert(smallint,0),
  6488.             NULLABLE = convert(smallint,0),
  6489.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6490.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6491.             SQL_DATA_TYPE = convert(smallint, NULL),
  6492.             SQL_DATETIME_SUB = convert(smallint,null),
  6493.             CHAR_OCTET_LENGTH = convert(int,null),
  6494.             ORDINAL_POSITION = convert(int,0),
  6495.             IS_NULLABLE = convert(varchar(254),'NO'),
  6496.             SS_DATA_TYPE = convert(tinyint,0)
  6497.         FROM
  6498.             syscomments c, sysobjects o
  6499.         WHERE
  6500.             o.name like @procedure_name
  6501.             AND user_name(o.uid) like @procedure_owner
  6502.             AND c.id = o.id
  6503.             AND c.colid = 1
  6504.             AND o.type IN ('TF', 'IF')
  6505.             AND '@TABLE_RETURN_VALUE' like @column_name
  6506.             AND c.number = 0
  6507.         ORDER BY 1, 2, 3, 18
  6508.     end
  6509. go
  6510.  
  6511. grant execute on sp_sproc_columns to public
  6512. go
  6513.  
  6514. dump tran master with no_log
  6515. go
  6516.  
  6517. print 'creating sp_statistics'
  6518. go
  6519.  
  6520. /*    Procedure for pre-7.0 server */
  6521. CREATE PROCEDURE sp_statistics (
  6522.                  @table_name        varchar(32),
  6523.                  @table_owner        varchar(32) = null,
  6524.                  @table_qualifier    varchar(32) = null,
  6525.                  @index_name        varchar(32) = '%',
  6526.                  @is_unique         char(1) = 'N',
  6527.                  @accuracy            char(1) = 'Q')
  6528. AS
  6529.     set nocount on
  6530.     DECLARE @indid                int
  6531.     DECLARE @lastindid            int
  6532.     DECLARE @table_id            int
  6533.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  6534.  
  6535.     create table #TmpIndex(
  6536.         TABLE_QUALIFIER varchar(32) NULL,
  6537.         TABLE_OWNER     varchar(32) NULL,
  6538.         TABLE_NAME        varchar(32) NOT NULL,
  6539.         INDEX_QUALIFIER varchar(32) null,
  6540.         INDEX_NAME        varchar(32) null,
  6541.         NON_UNIQUE        smallint null,
  6542.         TYPE            smallint NOT NULL,
  6543.         SEQ_IN_INDEX    smallint null,
  6544.         COLUMN_NAME     varchar(32) null,
  6545.         COLLATION        char(1) null,
  6546.         index_id        int null,
  6547.         CARDINALITY     int null,
  6548.         PAGES            int null,
  6549.         status            smallint NOT NULL)
  6550.  
  6551.     if @table_qualifier is not null
  6552.     begin
  6553.         if db_name() <> @table_qualifier
  6554.         begin    /* If qualifier doesn't match current database */
  6555.             raiserror 20001 '~~Rush_5~~'
  6556.             return
  6557.         end
  6558.     end
  6559.  
  6560.     if @accuracy not in ('Q','E')
  6561.         begin
  6562.             raiserror 20002 '~~Rush_58~~'
  6563.             return
  6564.         end
  6565.  
  6566.     if (@@trancount <> 0 and
  6567.         charindex('6.50', @@version) = 0 and
  6568.         charindex('7.00', @@version) = 0 and
  6569.         charindex('8.00', @@version) = 0)
  6570.     begin    /* If inside a transaction */
  6571.         raiserror 20003 '~~Rush_59~~'
  6572.         return
  6573.     end
  6574.  
  6575.     if @table_owner is null
  6576.     begin    /* If unqualified table name */
  6577.         SELECT @full_table_name = @table_name
  6578.     end
  6579.     else
  6580.     begin    /* Qualified table name */
  6581.         if @table_owner = ''
  6582.         begin    /* If empty owner name */
  6583.             SELECT @full_table_name = @table_owner
  6584.         end
  6585.         else
  6586.         begin
  6587.             SELECT @full_table_name = @table_owner + '.' + @table_name
  6588.         end
  6589.     end
  6590.     /*    Get Object ID */
  6591.     SELECT @table_id = object_id(@full_table_name)
  6592.  
  6593.     /*    Start at lowest index id */
  6594.     SELECT @indid = min(indid)
  6595.     FROM sysindexes
  6596.     WHERE id = @table_id
  6597.         AND indid > 0
  6598.         AND indid < 255
  6599.  
  6600.     WHILE @indid is not NULL
  6601.     BEGIN
  6602.         INSERT #TmpIndex    /* Add all columns that are in index */
  6603.             SELECT
  6604.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  6605.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  6606.                 o.name,                                 /* TABLE_NAME       */
  6607.                 o.name,                                 /* INDEX_QUALIFIER */
  6608.                 x.name,                                 /* INDEX_NAME       */
  6609.                 0,                                        /* NON_UNIQUE       */
  6610.                 1,                                        /* SQL_INDEX_CLUSTERED */
  6611.                 colid,                                    /* SEQ_IN_INDEX    */
  6612.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  6613.                 'A',                                    /* COLLATION       */
  6614.                 @indid,                                 /* index_id        */
  6615.                 x.rows,                                 /* CARDINALITY       */
  6616.                 x.dpages,                                /* PAGES           */
  6617.                 x.status                                /* status            */
  6618.             FROM sysindexes x, syscolumns c, sysobjects o
  6619.             WHERE
  6620.                 x.id = @table_id
  6621.                 AND x.id = o.id
  6622.                 AND x.id = c.id
  6623.                 AND c.colid < keycnt+(x.status&16)/16    /* all but Unique Clust indices have an extra key */
  6624.                 AND x.indid = @indid
  6625.         /*
  6626.         **      Now move @indid to the next index.
  6627.         */
  6628.         SELECT @lastindid = @indid
  6629.         SELECT @indid = NULL
  6630.  
  6631.         SELECT @indid = min(indid)
  6632.         FROM sysindexes
  6633.         WHERE id = @table_id
  6634.             AND indid > @lastindid
  6635.             AND indid < 255
  6636.     END
  6637.  
  6638.     UPDATE #TmpIndex
  6639.         SET NON_UNIQUE = 1
  6640.         WHERE status&2 <> 2 /* If non-unique index */
  6641.     UPDATE #TmpIndex
  6642.         SET
  6643.             TYPE = 3,            /* SQL_INDEX_OTHER */
  6644.             CARDINALITY = NULL,
  6645.             PAGES = NULL
  6646.         WHERE index_id > 1    /* If non-clustered index */
  6647.  
  6648.     /* now add row for table statistics */
  6649.     INSERT #TmpIndex
  6650.         SELECT
  6651.             DB_NAME(),                /* TABLE_QUALIFIER */
  6652.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  6653.             o.name,                 /* TABLE_NAME       */
  6654.             null,                    /* INDEX_QUALIFIER */
  6655.             null,                    /* INDEX_NAME       */
  6656.             null,                    /* NON_UNIQUE       */
  6657.             0,                        /* SQL_TABLE_STAT  */
  6658.             null,                    /* SEQ_IN_INDEX    */
  6659.             null,                    /* COLUMN_NAME       */
  6660.             null,                    /* COLLATION       */
  6661.             0,                        /* index_id        */
  6662.             x.rows,                 /* CARDINALITY       */
  6663.             x.dpages,                /* PAGES           */
  6664.             0                        /* status           */
  6665.         FROM sysindexes x, sysobjects o
  6666.         WHERE o.id = @table_id
  6667.             AND x.id = o.id
  6668.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  6669.                                                 /*    then table stats are in */
  6670.                                                 /*    a row with indid =0        */
  6671.  
  6672.     if @is_unique <> 'Y'    /* If all indexes desired */
  6673.         SELECT
  6674.             TABLE_QUALIFIER,
  6675.             TABLE_OWNER,
  6676.             TABLE_NAME,
  6677.             NON_UNIQUE,
  6678.             INDEX_QUALIFIER,
  6679.             INDEX_NAME,
  6680.             TYPE,
  6681.             SEQ_IN_INDEX,
  6682.             COLUMN_NAME,
  6683.             COLLATION,
  6684.             CARDINALITY,
  6685.             PAGES,
  6686.             FILTER_CONDITION = convert(varchar(128),null)
  6687.         FROM #TmpIndex
  6688.         WHERE
  6689.             INDEX_NAME like @index_name /* If matching name */
  6690.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  6691.         ORDER BY 4, 7, 6, 8
  6692.     else                    /* If only unique indexes desired */
  6693.         SELECT
  6694.             TABLE_QUALIFIER,
  6695.             TABLE_OWNER,
  6696.             TABLE_NAME,
  6697.             NON_UNIQUE,
  6698.             INDEX_QUALIFIER,
  6699.             INDEX_NAME,
  6700.             TYPE,
  6701.             SEQ_IN_INDEX,
  6702.             COLUMN_NAME,
  6703.             COLLATION,
  6704.             CARDINALITY,
  6705.             PAGES,
  6706.             FILTER_CONDITION = convert(varchar(128),null)
  6707.         FROM #TmpIndex
  6708.         WHERE
  6709.             (NON_UNIQUE = 0             /* If unique */
  6710.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  6711.             and (INDEX_NAME like @index_name    /* If matching name */
  6712.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  6713.         ORDER BY 4, 7, 6, 8
  6714.  
  6715.     DROP TABLE #TmpIndex
  6716. go
  6717.  
  6718. if (charindex('7.00', @@version) = 0 and
  6719.     charindex('8.00', @@version) = 0)
  6720. begin
  6721.     print ''
  6722.     print ''
  6723.     print 'Warning:'
  6724.     print 'you are installing the stored procedures '
  6725.     print 'on a pre 7.0 SQL Server.'
  6726.     print 'Ignore the following errors.'
  6727. end
  6728. else
  6729.     drop proc sp_statistics
  6730. go
  6731.  
  6732. /*    Procedure for 7.0 server */
  6733. CREATE PROCEDURE sp_statistics (
  6734.                  @table_name        sysname,
  6735.                  @table_owner        sysname = null,
  6736.                  @table_qualifier    sysname = null,
  6737.                  @index_name        sysname = '%',
  6738.                  @is_unique         char(1) = 'N',
  6739.                  @accuracy            char(1) = 'Q')
  6740. AS
  6741.     set nocount on
  6742.     DECLARE @indid                int
  6743.     DECLARE @lastindid            int
  6744.     DECLARE @table_id            int
  6745.     DECLARE @full_table_name    nvarchar(257)
  6746.  
  6747.     create table #TmpIndex(
  6748.         TABLE_QUALIFIER sysname NULL,
  6749.         TABLE_OWNER     sysname NULL,
  6750.         TABLE_NAME        sysname NOT NULL,
  6751.         INDEX_QUALIFIER sysname null,
  6752.         INDEX_NAME        sysname null,
  6753.         NON_UNIQUE        smallint null,
  6754.         TYPE            smallint NOT NULL,
  6755.         SEQ_IN_INDEX    smallint null,
  6756.         COLUMN_NAME     sysname null,
  6757.         COLLATION        char(1) null,
  6758.         index_id        int null,
  6759.         CARDINALITY     int null,
  6760.         PAGES            int null,
  6761.         status            int NOT NULL)
  6762.  
  6763.     if @table_qualifier is not null
  6764.     begin
  6765.         if db_name() <> @table_qualifier
  6766.         begin    /* If qualifier doesn't match current database */
  6767.             raiserror (15250, -1,-1)
  6768.             return
  6769.         end
  6770.     end
  6771.  
  6772.     if @accuracy not in ('Q','E')
  6773.         begin
  6774.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  6775.             return
  6776.         end
  6777.  
  6778.     if @table_owner is null
  6779.     begin    /* If unqualified table name */
  6780.         SELECT @full_table_name = quotename(@table_name)
  6781.     end
  6782.     else
  6783.     begin    /* Qualified table name */
  6784.         if @table_owner = ''
  6785.         begin    /* If empty owner name */
  6786.             SELECT @full_table_name = quotename(@table_owner)
  6787.         end
  6788.         else
  6789.         begin
  6790.             SELECT @full_table_name = quotename(@table_owner) +
  6791.                 '.' + quotename(@table_name)
  6792.         end
  6793.     end
  6794.     /*    Get Object ID */
  6795.     SELECT @table_id = object_id(@full_table_name)
  6796.  
  6797.     /*    Start at lowest index id */
  6798.     SELECT @indid = min(indid)
  6799.     FROM sysindexes
  6800.     WHERE not (@table_id is null)
  6801.         AND id = @table_id
  6802.         AND indid > 0
  6803.         AND indid < 255
  6804.  
  6805.     /* Create a temp table to correct the ordinal position of the columns */
  6806.     create table #TmpColumns
  6807.     (ordinal int identity(1,1),
  6808.      colid   smallint not null)
  6809.  
  6810.     /* Load columns into the temp table */
  6811.     insert into #TmpColumns (colid)
  6812.     select c.colid
  6813.     from syscolumns c
  6814.     where c.id = @table_id
  6815.     order by c.colid
  6816.     
  6817.     WHILE @indid is not NULL
  6818.     BEGIN
  6819.         INSERT #TmpIndex    /* Add all columns that are in index */
  6820.             SELECT
  6821.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  6822.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  6823.                 o.name,                                 /* TABLE_NAME       */
  6824.                 o.name,                                 /* INDEX_QUALIFIER */
  6825.                 x.name,                                 /* INDEX_NAME       */
  6826.                 case                                    /* NON_UNIQUE       */
  6827.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  6828.                     else 0                                /* Unique index    */
  6829.                 end,
  6830.                 case                                    /* TYPE            */
  6831.                     when @indid > 1 then 3                /* Non-Clustered   */
  6832.                     else 1                                /* Clustered index */
  6833.                 end,
  6834.                 tc.ordinal,                                    /* SEQ_IN_INDEX    */
  6835.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME       */
  6836.                 'A',                                    /* COLLATION       */
  6837.                 @indid,                                 /* index_id        */
  6838.                 case                                    /* CARDINALITY       */
  6839.                     when @indid > 1 then NULL            /* Non-Clustered   */
  6840.                     else x.rows                         /* Clustered index */
  6841.                 end,
  6842.                 case                                    /* PAGES           */
  6843.                     when @indid > 1 then NULL            /* Non-Clustered   */
  6844.                     else x.dpages                        /* Clustered index */
  6845.                 end,
  6846.                 x.status                                /* status            */
  6847.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  6848.             WHERE
  6849.                 not (@table_id is null)
  6850.                 AND x.id = @table_id
  6851.                 AND x.id = o.id
  6852.                 AND x.id = c.id
  6853.                 AND tc.colid = c.colid
  6854.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  6855.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  6856.                 AND indid = @indid
  6857.                 AND (x.status&2 = 2
  6858.                     OR @is_unique <> 'Y')
  6859.                 AND (x.status&32) = 0
  6860.         /*
  6861.         **      Now move @indid to the next index.
  6862.         */
  6863.         SELECT @lastindid = @indid
  6864.         SELECT @indid = NULL
  6865.  
  6866.         SELECT @indid = min(indid)
  6867.         FROM sysindexes
  6868.         WHERE not (@table_id is null)
  6869.             AND id = @table_id
  6870.             AND indid > @lastindid
  6871.             AND indid < 255
  6872.     END
  6873.  
  6874.     /* now add row for table statistics */
  6875.     INSERT #TmpIndex
  6876.         SELECT
  6877.             DB_NAME(),                /* TABLE_QUALIFIER */
  6878.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  6879.             o.name,                 /* TABLE_NAME       */
  6880.             null,                    /* INDEX_QUALIFIER */
  6881.             null,                    /* INDEX_NAME       */
  6882.             null,                    /* NON_UNIQUE       */
  6883.             0,                        /* SQL_TABLE_STAT  */
  6884.             null,                    /* SEQ_IN_INDEX    */
  6885.             null,                    /* COLUMN_NAME       */
  6886.             null,                    /* COLLATION       */
  6887.             0,                        /* index_id        */
  6888.             x.rows,                 /* CARDINALITY       */
  6889.             x.dpages,                /* PAGES           */
  6890.             0                        /* status           */
  6891.         FROM sysindexes x, sysobjects o
  6892.         WHERE not (@table_id is null)
  6893.             AND o.id = @table_id
  6894.             AND x.id = o.id
  6895.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  6896.                                                 /*    then table stats are in */
  6897.                                                 /*    a row with indid =0        */
  6898.  
  6899.     SELECT
  6900.         TABLE_QUALIFIER,
  6901.         TABLE_OWNER,
  6902.         TABLE_NAME,
  6903.         NON_UNIQUE,
  6904.         INDEX_QUALIFIER,
  6905.         INDEX_NAME,
  6906.         TYPE,
  6907.         SEQ_IN_INDEX,
  6908.         COLUMN_NAME,
  6909.         COLLATION,
  6910.         CARDINALITY,
  6911.         PAGES,
  6912.         FILTER_CONDITION = convert(varchar(128),null)
  6913.     FROM #TmpIndex
  6914.     WHERE
  6915.         INDEX_NAME like @index_name /* If matching name */
  6916.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  6917.     ORDER BY 4, 7, 6, 8
  6918.  
  6919.     DROP TABLE #TmpIndex, #TmpColumns
  6920. go
  6921.  
  6922. if (charindex('8.00', @@version) = 0)
  6923. begin
  6924.     print ''
  6925.     print ''
  6926.     print 'Warning:'
  6927.     print 'you are installing the stored procedures '
  6928.     print 'on a pre 8.0 SQL Server.'
  6929.     print 'Ignore the following errors.'
  6930. end
  6931. else
  6932.     drop proc sp_statistics
  6933. go
  6934.  
  6935. /*    Procedure for 8.0 server */
  6936. CREATE PROCEDURE sp_statistics (
  6937.                  @table_name        sysname,
  6938.                  @table_owner        sysname = null,
  6939.                  @table_qualifier    sysname = null,
  6940.                  @index_name        sysname = '%',
  6941.                  @is_unique         char(1) = 'N',
  6942.                  @accuracy            char(1) = 'Q')
  6943. AS
  6944.     set nocount on
  6945.     DECLARE @indid                int
  6946.     DECLARE @lastindid            int
  6947.     DECLARE @table_id            int
  6948.     DECLARE @full_table_name    nvarchar(257)
  6949.  
  6950.     create table #TmpIndex(
  6951.         TABLE_QUALIFIER sysname collate database_default NULL,
  6952.         TABLE_OWNER     sysname collate database_default NULL,
  6953.         TABLE_NAME        sysname collate database_default NOT NULL,
  6954.         INDEX_QUALIFIER sysname collate database_default null,
  6955.         INDEX_NAME        sysname collate database_default null,
  6956.         NON_UNIQUE        smallint null,
  6957.         TYPE            smallint NOT NULL,
  6958.         SEQ_IN_INDEX    smallint null,
  6959.         COLUMN_NAME     sysname collate database_default null,
  6960.         COLLATION        char(1) collate database_default null,
  6961.         index_id        int null,
  6962.         CARDINALITY     int null,
  6963.         PAGES            int null,
  6964.         status            int NOT NULL)
  6965.  
  6966.     if @table_qualifier is not null
  6967.     begin
  6968.         if db_name() <> @table_qualifier
  6969.         begin    /* If qualifier doesn't match current database */
  6970.             raiserror (15250, -1,-1)
  6971.             return
  6972.         end
  6973.     end
  6974.  
  6975.     if @accuracy not in ('Q','E')
  6976.         begin
  6977.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  6978.             return
  6979.         end
  6980.  
  6981.     if @table_owner is null
  6982.     begin    /* If unqualified table name */
  6983.         SELECT @full_table_name = quotename(@table_name)
  6984.     end
  6985.     else
  6986.     begin    /* Qualified table name */
  6987.         if @table_owner = ''
  6988.         begin    /* If empty owner name */
  6989.             SELECT @full_table_name = quotename(@table_owner)
  6990.         end
  6991.         else
  6992.         begin
  6993.             SELECT @full_table_name = quotename(@table_owner) +
  6994.                 '.' + quotename(@table_name)
  6995.         end
  6996.     end
  6997.     /*    Get Object ID */
  6998.     SELECT @table_id = object_id(@full_table_name)
  6999.  
  7000.     /*    Start at lowest index id */
  7001.     SELECT @indid = min(indid)
  7002.     FROM sysindexes
  7003.     WHERE not (@table_id is null)
  7004.         AND id = @table_id
  7005.         AND indid > 0
  7006.         AND indid < 255
  7007.  
  7008.     /* Create a temp table to correct the ordinal position of the columns */
  7009.     create table #TmpColumns
  7010.     (ordinal int identity(1,1),
  7011.      colid   smallint not null)
  7012.  
  7013.     /* Load columns into the temp table */
  7014.     insert into #TmpColumns (colid)
  7015.     select c.colid
  7016.     from syscolumns c
  7017.     where c.id = @table_id
  7018.     order by c.colid
  7019.     
  7020.     WHILE @indid is not NULL
  7021.     BEGIN
  7022.         INSERT #TmpIndex    /* Add all columns that are in index */
  7023.             SELECT
  7024.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  7025.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  7026.                 o.name,                                 /* TABLE_NAME       */
  7027.                 o.name,                                 /* INDEX_QUALIFIER */
  7028.                 x.name,                                 /* INDEX_NAME       */
  7029.                 case                                    /* NON_UNIQUE       */
  7030.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  7031.                     else 0                                /* Unique index    */
  7032.                 end,
  7033.                 case                                    /* TYPE            */
  7034.                     when @indid > 1 then 3                /* Non-Clustered   */
  7035.                     else 1                                /* Clustered index */
  7036.                 end,
  7037.                 tc.ordinal,                                    /* SEQ_IN_INDEX    */
  7038.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME       */
  7039.                 'A',                                    /* COLLATION       */
  7040.                 @indid,                                 /* index_id        */
  7041.                 case                                    /* CARDINALITY       */
  7042.                     when @indid > 1 then NULL            /* Non-Clustered   */
  7043.                     else x.rows                         /* Clustered index */
  7044.                 end,
  7045.                 case                                    /* PAGES           */
  7046.                     when @indid > 1 then NULL            /* Non-Clustered   */
  7047.                     else x.dpages                        /* Clustered index */
  7048.                 end,
  7049.                 x.status                                /* status            */
  7050.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  7051.             WHERE
  7052.                 not (@table_id is null)
  7053.                 AND x.id = @table_id
  7054.                 AND x.id = o.id
  7055.                 AND x.id = c.id
  7056.                 AND tc.colid = c.colid
  7057.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  7058.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  7059.                 AND indid = @indid
  7060.                 AND (x.status&2 = 2
  7061.                     OR @is_unique <> 'Y')
  7062.                 AND (x.status&32) = 0
  7063.         /*
  7064.         **      Now move @indid to the next index.
  7065.         */
  7066.         SELECT @lastindid = @indid
  7067.         SELECT @indid = NULL
  7068.  
  7069.         SELECT @indid = min(indid)
  7070.         FROM sysindexes
  7071.         WHERE not (@table_id is null)
  7072.             AND id = @table_id
  7073.             AND indid > @lastindid
  7074.             AND indid < 255
  7075.     END
  7076.  
  7077.     /* now add row for table statistics */
  7078.     INSERT #TmpIndex
  7079.         SELECT
  7080.             DB_NAME(),                /* TABLE_QUALIFIER */
  7081.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  7082.             o.name,                 /* TABLE_NAME       */
  7083.             null,                    /* INDEX_QUALIFIER */
  7084.             null,                    /* INDEX_NAME       */
  7085.             null,                    /* NON_UNIQUE       */
  7086.             0,                        /* SQL_TABLE_STAT  */
  7087.             null,                    /* SEQ_IN_INDEX    */
  7088.             null,                    /* COLUMN_NAME       */
  7089.             null,                    /* COLLATION       */
  7090.             0,                        /* index_id        */
  7091.             x.rows,                 /* CARDINALITY       */
  7092.             x.dpages,                /* PAGES           */
  7093.             0                        /* status           */
  7094.         FROM sysindexes x, sysobjects o
  7095.         WHERE not (@table_id is null)
  7096.             AND o.id = @table_id
  7097.             AND x.id = o.id
  7098.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  7099.                                                 /*    then table stats are in */
  7100.                                                 /*    a row with indid =0        */
  7101.  
  7102.     SELECT
  7103.         TABLE_QUALIFIER,
  7104.         TABLE_OWNER,
  7105.         TABLE_NAME,
  7106.         NON_UNIQUE,
  7107.         INDEX_QUALIFIER,
  7108.         INDEX_NAME,
  7109.         TYPE,
  7110.         SEQ_IN_INDEX,
  7111.         COLUMN_NAME,
  7112.         COLLATION,
  7113.         CARDINALITY,
  7114.         PAGES,
  7115.         FILTER_CONDITION = convert(varchar(128),null)
  7116.     FROM #TmpIndex
  7117.     WHERE
  7118.         INDEX_NAME like @index_name /* If matching name */
  7119.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  7120.     ORDER BY 4, 7, 6, 8
  7121.  
  7122.     DROP TABLE #TmpIndex, #TmpColumns
  7123. go
  7124.  
  7125. grant execute on sp_statistics to public
  7126. go
  7127.  
  7128. dump tran master with no_log
  7129. go
  7130.  
  7131. print 'creating sp_stored_procedures'
  7132. go
  7133.  
  7134.  
  7135. /* pre 8.0 version */
  7136. create procedure sp_stored_procedures(
  7137.                         @sp_name        varchar(102) = null,
  7138.                         @sp_owner        varchar(96) = null,
  7139.                         @sp_qualifier    varchar(32) = null)
  7140. as
  7141.     declare @proc_type smallint
  7142.  
  7143.     if @sp_qualifier is not null
  7144.     begin
  7145.         if db_name() <> @sp_qualifier
  7146.         begin
  7147.             if @sp_qualifier = ''
  7148.             begin
  7149.                 /* in this case, we need to return an empty result set */
  7150.                 /* because the user has requested a database with an empty name */
  7151.                 select @sp_name = ''
  7152.                 select @sp_owner = ''
  7153.             end else
  7154.             begin    /* If qualifier doesn't match current database */
  7155.                 raiserror 20001 '~~Rush_51~~'
  7156.                 return
  7157.             end
  7158.         end
  7159.     end
  7160.  
  7161.     if @sp_name is null
  7162.     begin  /*  If procedure name not supplied, match all */
  7163.         select @sp_name = '%'
  7164.     end
  7165.     else begin
  7166.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7167.         begin
  7168.             if exists (select * from sysobjects
  7169.                 where uid = user_id()
  7170.                     and name = @sp_name
  7171.                     and type = 'P') /* Object type of Procedure */
  7172.             begin
  7173.                 select @sp_owner = user_name()
  7174.             end
  7175.         end
  7176.     end
  7177.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  7178.         select @sp_owner = '%'
  7179.  
  7180.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  7181.  
  7182.     select
  7183.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  7184.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  7185.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  7186.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  7187.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7188.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  7189.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  7190.         PROCEDURE_TYPE = @proc_type
  7191.     from
  7192.         sysobjects o,syscomments c,sysusers u
  7193.     where
  7194.         o.name like @sp_name
  7195.         and c.colid = 1
  7196.         and user_name(o.uid) like @sp_owner
  7197.         and o.type = 'P'        /* Object type of Procedure */
  7198.         and c.id = o.id
  7199.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  7200.         and (suser_id() = 1     /* User is the System Administrator */
  7201.             or o.uid = user_id()    /* User created the object */
  7202.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  7203.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  7204.              from sysprotects p
  7205.              /* outer join to correlate with all rows in sysobjects */
  7206.              where p.id =* o.id
  7207.                  /*  get rows for public,current user,user's group */
  7208.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  7209.                  /* check for SELECT,EXECUTE privilege */
  7210.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  7211.             ) = 1     /* final magic...compare Grants    */
  7212.         )
  7213.     order by 1, 2, 3
  7214. go
  7215.  
  7216. grant execute on sp_stored_procedures to public
  7217. go
  7218.  
  7219.  
  7220. if (charindex('7.00', @@version) > 0 or
  7221.     charindex('8.00', @@version) > 0)
  7222.     drop procedure sp_stored_procedures
  7223. else
  7224. begin
  7225.     print ''
  7226.     print ''
  7227.     print 'Warning:'
  7228.     print 'you are installing the stored procedures '
  7229.     print 'on a pre 7.0 SQL Server.'
  7230.     print 'Ignore the following errors.'
  7231. end
  7232. go
  7233.  
  7234.  
  7235. /* 7.0 version */
  7236. create procedure sp_stored_procedures(
  7237.                         @sp_name        nvarchar(390) = null,
  7238.                         @sp_owner        nvarchar(384) = null,
  7239.                         @sp_qualifier    sysname = null)
  7240. as
  7241.     declare @proc_type smallint
  7242.  
  7243.     if @sp_qualifier is not null
  7244.     begin
  7245.         if db_name() <> @sp_qualifier
  7246.         begin
  7247.             if @sp_qualifier = ''
  7248.             begin
  7249.                 /* in this case, we need to return an empty result set */
  7250.                 /* because the user has requested a database with an empty name */
  7251.                 select @sp_name = ''
  7252.                 select @sp_owner = ''
  7253.             end else
  7254.             begin    /* If qualifier doesn't match current database */
  7255.                 raiserror (15250, -1,-1)
  7256.                 return
  7257.             end
  7258.         end
  7259.     end
  7260.  
  7261.     if @sp_name is null
  7262.     begin  /*  If procedure name not supplied, match all */
  7263.         select @sp_name = '%'
  7264.     end
  7265.     else begin
  7266.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7267.         begin
  7268.             if exists (select * from sysobjects
  7269.                 where uid = user_id()
  7270.                     and name = @sp_name
  7271.                     and type = 'P') /* Object type of Procedure */
  7272.             begin
  7273.                 select @sp_owner = user_name()
  7274.             end
  7275.         end
  7276.     end
  7277.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  7278.         select @sp_owner = '%'
  7279.  
  7280.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  7281.  
  7282.     select
  7283.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7284.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7285.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7286.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  7287.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7288.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  7289.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  7290.         PROCEDURE_TYPE = @proc_type
  7291.     from
  7292.         sysobjects o,syscomments c
  7293.     where
  7294.         o.name like @sp_name
  7295.         and c.colid = 1
  7296.         and user_name(o.uid) like @sp_owner
  7297.         and o.type = 'P'        /* Object type of Procedure */
  7298.         and c.id = o.id
  7299.         and permissions (o.id)&32 <> 0
  7300.     order by 1, 2, 3
  7301. go
  7302.  
  7303. grant execute on sp_stored_procedures to public
  7304. go
  7305.  
  7306. if (charindex('8.00', @@version) > 0)
  7307.     drop procedure sp_stored_procedures
  7308. else
  7309. begin
  7310.     print ''
  7311.     print ''
  7312.     print 'Warning:'
  7313.     print 'you are installing the stored procedures '
  7314.     print 'on a pre 8.0 SQL Server.'
  7315.     print 'Ignore the following errors.'
  7316. end
  7317. go
  7318.  
  7319.  
  7320. /* 8.0 version */
  7321. create procedure sp_stored_procedures(
  7322.                         @sp_name    nvarchar(390) = null,
  7323.                         @sp_owner    nvarchar(384) = null,
  7324.                         @sp_qualifier    sysname = null)
  7325. as
  7326.     declare @proc_type smallint
  7327.  
  7328.     if @sp_qualifier is not null
  7329.     begin
  7330.         if db_name() <> @sp_qualifier
  7331.         begin
  7332.             if @sp_qualifier = ''
  7333.             begin
  7334.                 /* in this case, we need to return an empty result set */
  7335.                 /* because the user has requested a database with an empty name */
  7336.                 select @sp_name = ''
  7337.                 select @sp_owner = ''
  7338.             end else
  7339.             begin    /* If qualifier doesn't match current database */
  7340.                 raiserror (15250, -1,-1)
  7341.                 return
  7342.             end
  7343.         end
  7344.     end
  7345.  
  7346.     if @sp_name is null
  7347.     begin  /*  If procedure name not supplied, match all */
  7348.         select @sp_name = '%'
  7349.     end
  7350.     else
  7351.     begin
  7352.     if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7353.     begin
  7354.         if exists (select * from sysobjects
  7355.             where uid = user_id()
  7356.                 and name = @sp_name
  7357.                 and type in ('P', 'FN', 'TF', 'IF'))        /* Procedures, scalar UDF, table UDF */
  7358.         begin
  7359.             select @sp_owner = user_name()
  7360.         end
  7361.     end
  7362.     end
  7363.     
  7364.     if @sp_owner is null        /* If procedure owner not supplied, match all */
  7365.     select @sp_owner = '%'
  7366.  
  7367.     select @proc_type=2            /* Return 2 for 4.2 and later servers. */
  7368.  
  7369.     select
  7370.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7371.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7372.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7373.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  7374.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7375.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  7376.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  7377.         PROCEDURE_TYPE = @proc_type
  7378.     from
  7379.         sysobjects o,syscomments c
  7380.     where
  7381.         o.name like @sp_name
  7382.         and c.colid = 1
  7383.         and user_name(o.uid) like @sp_owner
  7384.         and o.type in ('P', 'FN', 'TF', 'IF')        /* Object type of Procedure, scalar UDF, table UDF */
  7385.         and c.id = o.id
  7386.         and permissions (o.id)&32 <> 0
  7387.     order by 1, 2, 3
  7388. go
  7389.  
  7390. grant execute on sp_stored_procedures to public
  7391. go
  7392.  
  7393. dump tran master with no_log
  7394. go
  7395.  
  7396.  
  7397. print 'creating sp_table_privileges'
  7398. go
  7399.  
  7400. /*    Procedure for pre 6.50 server */
  7401. CREATE PROCEDURE sp_table_privileges (
  7402.             @table_name         varchar(90),
  7403.             @table_owner        varchar(90) = null,
  7404.             @table_qualifier    varchar(32) = null)
  7405. as
  7406.     set nocount on
  7407.  
  7408.     declare @table_id    int,
  7409.             @owner_id     int,
  7410.             @full_table_name char(181)
  7411.     declare @refconst int
  7412.  
  7413.     select @refconst = 1
  7414.     if    (charindex('6.00', @@version) = 0)
  7415.         select @refconst = NULL
  7416.  
  7417.     if @table_qualifier is not null
  7418.     begin
  7419.         if db_name() != @table_qualifier
  7420.         begin    /* If qualifier doesn't match current database */
  7421.             raiserror 20001 'Table qualifier must be name of current database'
  7422.             return
  7423.         end
  7424.     end
  7425.     if @table_owner is null
  7426.     begin    /* If unqualified table name */
  7427.         SELECT @full_table_name = @table_name
  7428.     end
  7429.     else
  7430.     begin    /* Qualified table name */
  7431.         SELECT @full_table_name = @table_owner + '.' + @table_name
  7432.     end
  7433.     /*    Get Object ID */
  7434.     SELECT @table_id = object_id(@full_table_name)
  7435.  
  7436.     if @@trancount != 0
  7437.     begin    /* If inside a transaction */
  7438.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  7439.         return
  7440.     end
  7441.     create table #table_priv1(
  7442.         table_qualifier            varchar(32) NOT NULL,
  7443.         table_owner             varchar(32) NOT NULL,
  7444.         table_name                varchar(32) NOT NULL,
  7445.         grantor                 varchar(32) NOT NULL,
  7446.         grantee                 varchar(32) NOT NULL,
  7447.         select_privilege        int NOT NULL,
  7448.         insert_privilege        int NOT NULL,
  7449.         update_privilege        int NOT NULL,
  7450.         delete_privilege        int NOT NULL,
  7451.         references_privilege    int NULL,
  7452.         is_grantable            varchar(3) NOT NULL,
  7453.         uid                     int NOT NULL,
  7454.         gid                     int NOT NULL)
  7455.  
  7456.     insert into #table_priv1
  7457.         select distinct
  7458.             db_name(),
  7459.             user_name(o.uid),
  7460.             o.name,
  7461.             user_name(o.uid),
  7462.             u.name,
  7463.             0,
  7464.             0,
  7465.             0,
  7466.             0,
  7467.             @refconst,
  7468.             'no',
  7469.             u.uid,
  7470.             u.gid
  7471.         from sysusers u, sysobjects o
  7472.         where o.id = @table_id and u.uid != u.gid
  7473.            and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  7474.                                            ** user tables, and views. */
  7475.  
  7476.     /*
  7477.     ** now add row for table owner
  7478.     */
  7479.     if exists (
  7480.         select *
  7481.             from #table_priv1
  7482.             where grantor = grantee)
  7483.     begin
  7484.         update #table_priv1
  7485.         set
  7486.             select_privilege = 1,
  7487.             update_privilege = 1,
  7488.             insert_privilege = 1,
  7489.             delete_privilege = 1,
  7490.             references_privilege = 1,
  7491.             is_grantable = 'yes'
  7492.         where grantor = grantee
  7493.     end
  7494.     else
  7495.     begin
  7496.         insert into #table_priv1
  7497.             select    db_name(),
  7498.                 user_name(o.uid),
  7499.                 o.name,
  7500.                 user_name(o.uid),
  7501.                 user_name(o.uid),
  7502.                 1,
  7503.                 1,
  7504.                 1,
  7505.                 1,
  7506.                 @refconst,
  7507.                 'yes',
  7508.                 o.uid,
  7509.                 u.gid
  7510.             from sysobjects o, sysusers u
  7511.             where o.id = @table_id and u.uid = o.uid
  7512.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  7513.                                            ** user tables, and views. */
  7514.  
  7515.     end
  7516.  
  7517.     update #table_priv1
  7518.     set select_privilege = 1
  7519.     where
  7520.         exists (
  7521.             select * from sysprotects
  7522.             where
  7523.                 id = @table_id
  7524.                 and (#table_priv1.uid = uid
  7525.                     or #table_priv1.gid = uid
  7526.                     or uid = 0)
  7527.                 and protecttype = 205
  7528.                 and action = 193)
  7529.         and not exists (
  7530.             select * from sysprotects
  7531.             where
  7532.                 id = @table_id
  7533.                 and (#table_priv1.uid = uid
  7534.                     or #table_priv1.gid = uid
  7535.                     or uid = 0)
  7536.                 and protecttype = 206
  7537.                 and action = 193)
  7538.  
  7539.     update #table_priv1
  7540.     set insert_privilege = 1
  7541.     where
  7542.         exists (
  7543.             select * from sysprotects
  7544.             where
  7545.                 id = @table_id
  7546.                 and (#table_priv1.uid = uid
  7547.                     or #table_priv1.gid = uid
  7548.                     or uid = 0)
  7549.                 and protecttype = 205
  7550.                 and action = 195)
  7551.         and not exists (
  7552.             select * from sysprotects
  7553.             where
  7554.                 id = @table_id
  7555.                 and (#table_priv1.uid = uid
  7556.                     or #table_priv1.gid = uid
  7557.                     or uid = 0)
  7558.                 and protecttype = 206
  7559.                 and action = 195)
  7560.  
  7561.     update #table_priv1
  7562.     set delete_privilege = 1
  7563.     where
  7564.         exists (
  7565.             select * from sysprotects
  7566.             where
  7567.                 id = @table_id
  7568.                 and (#table_priv1.uid = uid
  7569.                     or #table_priv1.gid = uid
  7570.                     or uid = 0)
  7571.                 and protecttype = 205
  7572.                 and action = 196)
  7573.         and not exists (select * from sysprotects
  7574.             where
  7575.                 id = @table_id
  7576.                 and (#table_priv1.uid = uid
  7577.                     or #table_priv1.gid = uid
  7578.                     or uid = 0)
  7579.                 and protecttype = 206
  7580.                 and action = 196)
  7581.  
  7582.     update #table_priv1
  7583.     set update_privilege = 1
  7584.     where
  7585.         exists (
  7586.             select * from sysprotects
  7587.             where
  7588.                 id = @table_id
  7589.                 and (#table_priv1.uid = uid
  7590.                     or #table_priv1.gid = uid
  7591.                     or uid = 0)
  7592.                 and protecttype = 205
  7593.                 and action = 197)
  7594.         and not exists (
  7595.             select * from sysprotects
  7596.             where
  7597.                 id = @table_id
  7598.                 and (#table_priv1.uid = uid
  7599.                     or #table_priv1.gid = uid
  7600.                     or uid = 0)
  7601.                 and protecttype = 206
  7602.                 and action = 197)
  7603.  
  7604.     update #table_priv1
  7605.     set references_privilege = 1
  7606.     where
  7607.         exists (
  7608.             select * from sysprotects
  7609.             where
  7610.                 id = @table_id
  7611.                 and (#table_priv1.uid = uid
  7612.                     or #table_priv1.gid = uid
  7613.                     or uid = 0)
  7614.                 and protecttype = 205
  7615.                 and action = 26)
  7616.         and not exists (
  7617.             select * from sysprotects
  7618.             where
  7619.                 id = @table_id
  7620.                 and (#table_priv1.uid = uid
  7621.                     or #table_priv1.gid = uid
  7622.                     or uid = 0)
  7623.                 and protecttype = 206
  7624.                 and action = 26)
  7625.  
  7626.     create table #table_priv2(
  7627.         table_qualifier varchar(32) NULL,
  7628.         table_owner     varchar(32) NULL,
  7629.         table_name        varchar(32) NOT NULL,
  7630.         grantor         varchar(32) NULL,
  7631.         grantee         varchar(32) NOT NULL,
  7632.         privilege        varchar(32) NOT NULL,
  7633.         is_grantable    varchar(3) NULL)
  7634.  
  7635.     insert into #table_priv2
  7636.         select
  7637.             table_qualifier,
  7638.             table_owner,
  7639.             table_name,
  7640.             grantor,
  7641.             grantee,
  7642.             'SELECT',
  7643.             is_grantable
  7644.         from #table_priv1
  7645.         where select_privilege = 1
  7646.  
  7647.  
  7648.     insert into #table_priv2
  7649.         select
  7650.             table_qualifier,
  7651.             table_owner,
  7652.             table_name,
  7653.             grantor,
  7654.             grantee,
  7655.             'INSERT',
  7656.             is_grantable
  7657.         from #table_priv1
  7658.         where insert_privilege = 1
  7659.  
  7660.  
  7661.     insert into #table_priv2
  7662.         select
  7663.             table_qualifier,
  7664.             table_owner,
  7665.             table_name,
  7666.             grantor,
  7667.             grantee,
  7668.             'DELETE',
  7669.             is_grantable
  7670.         from #table_priv1
  7671.         where delete_privilege = 1
  7672.  
  7673.  
  7674.     insert into #table_priv2
  7675.         select
  7676.             table_qualifier,
  7677.             table_owner,
  7678.             table_name,
  7679.             grantor,
  7680.             grantee,
  7681.             'UPDATE',
  7682.             is_grantable
  7683.         from #table_priv1
  7684.         where update_privilege = 1
  7685.  
  7686.     insert into #table_priv2
  7687.         select
  7688.             table_qualifier,
  7689.             table_owner,
  7690.             table_name,
  7691.             grantor,
  7692.             grantee,
  7693.             'REFERENCES',
  7694.             is_grantable
  7695.         from #table_priv1
  7696.         where references_privilege = 1
  7697.  
  7698.  
  7699.     select * from #table_priv2
  7700.     order by table_owner,table_name,privilege,grantee
  7701. /*    order by 2,3,6,5 Can't use since fails on 4.21a server */
  7702. go
  7703.  
  7704. if (charindex('6.50', @@version) = 0 and
  7705.     charindex('7.00', @@version) = 0 and
  7706.     charindex('8.00', @@version) = 0)
  7707. begin
  7708.     print ''
  7709.     print ''
  7710.     print 'Warning:'
  7711.     print 'you are installing the stored procedures '
  7712.     print 'on a pre 6.50 SQL Server.'
  7713.     print 'Ignore the following errors.'
  7714. end
  7715. else
  7716.     drop proc sp_table_privileges
  7717. go
  7718.  
  7719.  
  7720. /*    Procedure for 6.50 server */
  7721. CREATE PROCEDURE sp_table_privileges (
  7722.             @table_name         varchar(96),
  7723.             @table_owner        varchar(96) = null,
  7724.             @table_qualifier    varchar(32) = null)
  7725. as
  7726.  
  7727.     if @table_qualifier is not null
  7728.     begin
  7729.         if db_name() <> @table_qualifier
  7730.         begin    /* If qualifier doesn't match current database */
  7731.             raiserror (15250, -1,-1)
  7732.             return
  7733.         end
  7734.     end
  7735.     if @table_name is null
  7736.         select @table_name = '%'
  7737.     if @table_owner is null /* If no owner supplied, force wildcard */
  7738.         select @table_owner = '%'
  7739.  
  7740.     select
  7741.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7742.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7743.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7744.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  7745.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  7746.         convert(varchar(32),case p.action
  7747.              when 193 then 'SELECT'
  7748.              when 195 then 'INSERT'
  7749.              when 196 then 'DELETE'
  7750.              when 197 then 'UPDATE'
  7751.              else 'REFERENCES'
  7752.         end) PRIVILEGE,
  7753.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7754.             else 'YES'
  7755.         end) IS_GRANTABLE
  7756.     from sysprotects p, sysobjects o, sysusers u
  7757.     where
  7758.         p.id = o.id
  7759.         and o.type in ('U','V','S')
  7760.         and object_name(o.id) like @table_name
  7761.         and user_name(o.uid) like @table_owner
  7762.             /* expand groups */
  7763.         and ((p.uid = u.uid and u.uid <> u.gid) or
  7764.              (p.uid = u.gid and u.uid <> u.gid))
  7765.         and p.protecttype <> 206    /* only grant rows */
  7766.         and p.action in (26,193,195,196,197)
  7767.         and o.uid <> u.uid            /* no rows for owner */
  7768.         and not exists (            /* exclude revoke'd privileges */
  7769.             select *
  7770.             from sysprotects p1
  7771.             where
  7772.                 p1.protecttype = 206
  7773.                 and p1.action = p.action
  7774.                 and p1.id = p.id
  7775.                 and p1.uid = u.uid)
  7776.     union
  7777.     select    /*    Add rows for table owner */
  7778.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7779.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7780.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7781.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  7782.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  7783.         convert(varchar(32),case v.number
  7784.             when 193 then 'SELECT'
  7785.             when 195 then 'INSERT'
  7786.             when 196 then 'DELETE'
  7787.             when 197 then 'UPDATE'
  7788.             else 'REFERENCES'
  7789.         end) PRIVILEGE,
  7790.         convert(varchar(3),'YES') IS_GRANTABLE
  7791.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7792.     where
  7793.         object_name(o.id) like @table_name
  7794.         and o.type in ('U','V','S')
  7795.         and user_name(o.uid) like @table_owner
  7796.         and u.uid = 1        /* grantor is dbo of database */
  7797.         and v.type = 'P'    /* cross product to get all exposed privileges */
  7798.         and v.number in (26,193,195,196,197)
  7799.         and not exists (    /* exclude revoke'd privileges */
  7800.             select *
  7801.             from sysprotects p1
  7802.             where
  7803.                 p1.protecttype = 206
  7804.                 and p1.action = v.number
  7805.                 and p1.id = o.id
  7806.                 and p1.uid = o.uid)
  7807.     order by 2,3,6,5
  7808. go
  7809.  
  7810.  
  7811. if (charindex('7.00', @@version) > 0 or
  7812.     charindex('8.00', @@version) > 0)
  7813.     drop proc sp_table_privileges
  7814. else
  7815. begin
  7816.     print ''
  7817.     print ''
  7818.     print 'Warning:'
  7819.     print 'you are installing the stored procedures '
  7820.     print 'on a pre 7.0 SQL Server.'
  7821.     print 'Ignore the following errors.'
  7822. end
  7823. go
  7824.  
  7825. /*    Procedure for 7.0 and later servers */
  7826. CREATE PROCEDURE sp_table_privileges (
  7827.             @table_name         nvarchar(384),
  7828.             @table_owner        nvarchar(384) = null,
  7829.             @table_qualifier    sysname = null)
  7830. as
  7831.  
  7832.     if @table_qualifier is not null
  7833.     begin
  7834.         if db_name() <> @table_qualifier
  7835.         begin    /* If qualifier doesn't match current database */
  7836.             raiserror (15250, -1,-1)
  7837.             return
  7838.         end
  7839.     end
  7840.     if @table_name is null
  7841.         select @table_name = '%'
  7842.     if @table_owner is null /* If no owner supplied, force wildcard */
  7843.         select @table_owner = '%'
  7844.  
  7845.     select
  7846.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7847.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7848.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7849.         convert(sysname,user_name(p.grantor)) GRANTOR,
  7850.         convert(sysname,user_name(u.uid)) GRANTEE,
  7851.         convert(varchar(32),case p.action
  7852.              when 193 then 'SELECT'
  7853.              when 195 then 'INSERT'
  7854.              when 196 then 'DELETE'
  7855.              when 197 then 'UPDATE'
  7856.              else 'REFERENCES'
  7857.         end) PRIVILEGE,
  7858.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7859.             else 'YES'
  7860.         end) IS_GRANTABLE
  7861.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  7862.     where
  7863.         p.id = o.id
  7864.         and o.type in ('U','V','S')
  7865.         and object_name(o.id) like @table_name
  7866.         and user_name(o.uid) like @table_owner
  7867.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  7868.         and (u.uid > 0 and u.uid < 16384)
  7869.         and ((p.uid = u.uid) or
  7870.              (p.uid = m.groupuid and u.uid = m.memberuid))
  7871.         and p.protecttype <> 206    /* only grant rows */
  7872.         and p.action in (26,193,195,196,197)
  7873.         and o.uid <> u.uid            /* no rows for owner */
  7874.         and not exists (            /* exclude revoke'd privileges */
  7875.             select *
  7876.             from sysprotects p1
  7877.             where
  7878.                 p1.protecttype = 206
  7879.                 and p1.action = p.action
  7880.                 and p1.id = p.id
  7881.                 and p1.uid = u.uid)
  7882.     union
  7883.     select    /*    Add rows for table owner */
  7884.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7885.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7886.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7887.         convert(sysname,user_name(u.uid)) GRANTOR,
  7888.         convert(sysname,user_name(o.uid)) GRANTEE,
  7889.         convert(varchar(32),case v.number
  7890.             when 193 then 'SELECT'
  7891.             when 195 then 'INSERT'
  7892.             when 196 then 'DELETE'
  7893.             when 197 then 'UPDATE'
  7894.             else 'REFERENCES'
  7895.         end) PRIVILEGE,
  7896.         convert(varchar(3),'YES') IS_GRANTABLE
  7897.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7898.     where
  7899.         object_name(o.id) like @table_name
  7900.         and o.type in ('U','V','S')
  7901.         and user_name(o.uid) like @table_owner
  7902.         and u.uid = 1        /* grantor is 'dbo' of database */
  7903.         and v.type = N'P'    /* cross product to get all exposed privileges */
  7904.         and v.number in (26,193,195,196,197)
  7905.         and not exists (    /* exclude revoke'd privileges */
  7906.             select *
  7907.             from sysprotects p1
  7908.             where
  7909.                 p1.protecttype = 206
  7910.                 and p1.action = v.number
  7911.                 and p1.id = o.id
  7912.                 and p1.uid = o.uid)
  7913.     order by 2,3,6,5
  7914. go
  7915.  
  7916. grant execute on sp_table_privileges to public
  7917. go
  7918.  
  7919. dump tran master with no_log
  7920. go
  7921.  
  7922. print 'creating sp_tables'
  7923. go
  7924.  
  7925. /*    Procedure for 6.50 and earlier servers */
  7926. create procedure sp_tables(
  7927.                @table_name        varchar(96)    = null,
  7928.                @table_owner     varchar(96)    = null,
  7929.                @table_qualifier varchar(32)    = null,
  7930.                @table_type        varchar(100) = null)
  7931. as
  7932.     declare @type1 varchar(3)
  7933.     declare @tableindex int
  7934.  
  7935.  
  7936.     /* Special feature #1:    enumerate databases when owner and name
  7937.          are blank but qualifier is explicitly '%'.  */
  7938.     if @table_qualifier = '%' and
  7939.         @table_owner = '' and
  7940.         @table_name = ''
  7941.     begin    /* If enumerating databases */
  7942.         select
  7943.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  7944.             TABLE_OWNER = convert(varchar(32),null),
  7945.             TABLE_NAME = convert(varchar(32),null),
  7946.             TABLE_TYPE = convert(varchar(32),null),
  7947.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7948.         from master.dbo.sysdatabases d
  7949.         where d.name <> 'model'    /* eliminate MODEL database */
  7950.         order by 1
  7951.     end
  7952.  
  7953.     /* Special feature #2:    enumerate owners when qualifier and name
  7954.          are blank but owner is explicitly '%'.  */
  7955.     else if @table_qualifier = '' and
  7956.         @table_owner = '%' and
  7957.         @table_name = ''
  7958.     begin    /* If enumerating owners */
  7959.         select distinct
  7960.             TABLE_QUALIFIER = convert(varchar(32),null),
  7961.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  7962.             TABLE_NAME = convert(varchar(32),null),
  7963.             TABLE_TYPE = convert(varchar(32),null),
  7964.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7965.         from sysobjects
  7966.         order by 2
  7967.     end
  7968.  
  7969.     /* Special feature #3:    enumerate table types when qualifier, owner and
  7970.          name are blank but table type is explicitly '%'.    */
  7971.     else if @table_qualifier = '' and
  7972.         @table_owner = '' and
  7973.         @table_name = '' and
  7974.         @table_type = '%'
  7975.     begin    /* If enumerating table types */
  7976.         select
  7977.             TABLE_QUALIFIER = convert(varchar(32),null),
  7978.             TABLE_OWNER = convert(varchar(32),null),
  7979.             TABLE_NAME = convert(varchar(32),null),
  7980.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  7981.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7982.         from sysobjects o, syscolumns c
  7983.         where o.id=c.id and o.name='sysusers' and colid<=3
  7984.     end
  7985.  
  7986.     else
  7987.     begin /* end of special features - do normal processing */
  7988.         if @table_qualifier is not null
  7989.         begin
  7990.             if db_name() <> @table_qualifier
  7991.             begin
  7992.                 if @table_qualifier = ''
  7993.                 begin  /* If empty qualifier supplied */
  7994.                     /* Force an empty result set */
  7995.                     select @table_name = ''
  7996.                     select @table_owner = ''
  7997.                 end
  7998.                 else
  7999.                 begin    /* If qualifier doesn't match current database */
  8000.                     raiserror 20001 '~~Rush_5~~'
  8001.                     return
  8002.                 end
  8003.             end
  8004.         end
  8005.         if @table_type is null
  8006.         begin    /* Select all ODBC supported table types */
  8007.             select @type1 = 'SUV'
  8008.         end
  8009.         else
  8010.         begin
  8011.             /*    TableType is case sensitive if CS server */
  8012.             select @type1 = null
  8013.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8014.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8015.             if (charindex('''TABLE''',@table_type) <> 0)
  8016.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8017.             if (charindex('''VIEW''',@table_type) <> 0)
  8018.                 select @type1 = @type1 + 'V'    /* Add Views */
  8019.         end
  8020.         if @table_name is null
  8021.         begin    /*    If table name not supplied, match all */
  8022.             select @table_name = '%'
  8023.         end
  8024.         else
  8025.         begin
  8026.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8027.             begin    /* If owner not specified and table is specified */
  8028.                 if exists (select * from sysobjects
  8029.                     where uid = user_id()
  8030.                     and name = @table_name
  8031.                     and (type = 'U' or type = 'V' or type = 'S'))
  8032.                 begin    /* Override supplied owner w/owner of table */
  8033.                     select @table_owner = user_name()
  8034.                 end
  8035.             end
  8036.         end
  8037.         if @table_owner is null /* If no owner supplied, force wildcard */
  8038.             select @table_owner = '%'
  8039.         select
  8040.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  8041.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  8042.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  8043.             TABLE_TYPE = convert(varchar(32),rtrim(
  8044.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8045.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  8046.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8047.         from sysusers u, sysobjects o
  8048.         where
  8049.             o.name like @table_name
  8050.             and user_name(o.uid) like @table_owner
  8051.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8052.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  8053.             and (
  8054.                 suser_id() = 1     /* User is the System Administrator */
  8055.                 or o.uid = user_id()     /* User created the object */
  8056.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  8057.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  8058.                     from sysprotects p
  8059.                     /* outer join to correlate with all rows in sysobjects */
  8060.                     where p.id =* o.id
  8061.                         /* get rows for public,current user,user's group */
  8062.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  8063.                         /* check for SELECT,EXECUTE privilege */
  8064.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  8065.                     ) = 1    /* final magic...compare Grants      */
  8066.             )
  8067.         order by 4, 1, 2, 3
  8068.     end
  8069. go
  8070.  
  8071. if (charindex('7.00', @@version) > 0 or
  8072.     charindex('8.00', @@version) > 0)
  8073.     drop procedure sp_tables
  8074. else
  8075. begin
  8076.     print ''
  8077.     print ''
  8078.     print 'Warning:'
  8079.     print 'you are installing the stored procedures '
  8080.     print 'on a pre 8.0 SQL Server.'
  8081.     print 'Ignore the following errors.'
  8082. end
  8083. go
  8084.  
  8085. /*    Procedure for 8.0 server */
  8086. create procedure sp_tables(
  8087.                @table_name        nvarchar(384)    = null,
  8088.                @table_owner     nvarchar(384)    = null,
  8089.                @table_qualifier sysname    = null,
  8090.                @table_type        varchar(100) = null)
  8091. as
  8092.     declare @type1 varchar(3)
  8093.     declare @tableindex int
  8094.  
  8095.  
  8096.     /* Special feature #1:    enumerate databases when owner and name
  8097.          are blank but qualifier is explicitly '%'.  */
  8098.     if @table_qualifier = '%' and
  8099.         @table_owner = '' and
  8100.         @table_name = ''
  8101.     begin    /* If enumerating databases */
  8102.         select
  8103.             TABLE_QUALIFIER = convert(sysname,d.name),
  8104.             TABLE_OWNER = convert(sysname,null),
  8105.             TABLE_NAME = convert(sysname,null),
  8106.             TABLE_TYPE = convert(varchar(32),null),
  8107.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8108.         from master.dbo.sysdatabases d
  8109.         where d.name <> 'model'    /* eliminate MODEL database */
  8110.         order by 1
  8111.     end
  8112.  
  8113.     /* Special feature #2:    enumerate owners when qualifier and name
  8114.          are blank but owner is explicitly '%'.  */
  8115.     else if @table_qualifier = '' and
  8116.         @table_owner = '%' and
  8117.         @table_name = ''
  8118.     begin    /* If enumerating owners */
  8119.         select distinct
  8120.             TABLE_QUALIFIER = convert(sysname,null),
  8121.             TABLE_OWNER = convert(sysname,user_name(uid)),
  8122.             TABLE_NAME = convert(sysname,null),
  8123.             TABLE_TYPE = convert(varchar(32),null),
  8124.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8125.         from sysobjects
  8126.         order by 2
  8127.     end
  8128.  
  8129.     /* Special feature #3:    enumerate table types when qualifier, owner and
  8130.          name are blank but table type is explicitly '%'.    */
  8131.     else if @table_qualifier = '' and
  8132.         @table_owner = '' and
  8133.         @table_name = '' and
  8134.         @table_type = '%'
  8135.     begin    /* If enumerating table types */
  8136.         select
  8137.             TABLE_QUALIFIER = convert(sysname,null),
  8138.             TABLE_OWNER = convert(sysname,null),
  8139.             TABLE_NAME = convert(sysname,null),
  8140.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  8141.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8142.         from sysobjects o, syscolumns c
  8143.         where o.id=c.id and o.name='sysusers' and colid<=3
  8144.     end
  8145.  
  8146.     else
  8147.     begin /* end of special features - do normal processing */
  8148.         if @table_qualifier is not null
  8149.         begin
  8150.             if db_name() <> @table_qualifier
  8151.             begin
  8152.                 if @table_qualifier = ''
  8153.                 begin  /* If empty qualifier supplied */
  8154.                     /* Force an empty result set */
  8155.                     select @table_name = ''
  8156.                     select @table_owner = ''
  8157.                 end
  8158.                 else
  8159.                 begin    /* If qualifier doesn't match current database */
  8160.                     raiserror (15250, -1,-1)
  8161.                     return
  8162.                 end
  8163.             end
  8164.         end
  8165.         if @table_type is null
  8166.         begin    /* Select all ODBC supported table types */
  8167.             select @type1 = 'SUV'
  8168.         end
  8169.         else
  8170.         begin
  8171.             /*    TableType is case sensitive if CS server */
  8172.             select @type1 = ''
  8173.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8174.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8175.             if (charindex('''TABLE''',@table_type) <> 0)
  8176.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8177.             if (charindex('''VIEW''',@table_type) <> 0)
  8178.                 select @type1 = @type1 + 'V'    /* Add Views */
  8179.         end
  8180.         if @table_name is null
  8181.         begin    /*    If table name not supplied, match all */
  8182.             select @table_name = '%'
  8183.         end
  8184.         else
  8185.         begin
  8186.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8187.             begin    /* If owner not specified and table is specified */
  8188.                 if exists (select * from sysobjects
  8189.                     where uid = user_id()
  8190.                     and name = @table_name
  8191.                     and (type = 'U' or type = 'V' or type = 'S'))
  8192.                 begin    /* Override supplied owner w/owner of table */
  8193.                     select @table_owner = user_name()
  8194.                 end
  8195.             end
  8196.         end
  8197.         if @table_owner is null /* If no owner supplied, force wildcard */
  8198.             select @table_owner = '%'
  8199.         select
  8200.             TABLE_QUALIFIER = convert(sysname,db_name()),
  8201.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  8202.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  8203.             TABLE_TYPE = convert(varchar(32),rtrim(
  8204.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8205.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  8206.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8207.         from sysobjects o
  8208.         where
  8209.             o.name like @table_name
  8210.             and user_name(o.uid) like @table_owner
  8211.             and o.type in ('U','V','S')
  8212.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8213.             and permissions (o.id)&4096 <> 0
  8214.         order by 4, 1, 2, 3
  8215.     end
  8216. go
  8217.  
  8218.  
  8219. grant execute on sp_tables to public
  8220. go
  8221.  
  8222. dump tran master with no_log
  8223. go
  8224.  
  8225. /*-----------------------------------------------------------------------------*/
  8226. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  8227. /*-----------------------------------------------------------------------------*/
  8228.  
  8229. if object_id('sp_linkedservers', 'P') is not null
  8230.     drop proc sp_linkedservers
  8231. go
  8232. raiserror(15339,-1,-1,'sp_linkedservers')
  8233. go
  8234. create proc sp_linkedservers as
  8235.     select
  8236.         SRV_NAME = srvname,
  8237.         SRV_PROVIDERNAME = providername,
  8238.         SRV_PRODUCT = srvproduct,
  8239.         SRV_DATASOURCE = datasource,
  8240.         SRV_PROVIDERSTRING = providerstring,
  8241.         SRV_LOCATION = location,
  8242.         SRV_CAT = catalog
  8243.     from master.dbo.sysservers
  8244.     order by 1
  8245. go
  8246. grant execute on sp_linkedservers to public
  8247. go
  8248.  
  8249.  
  8250. if object_id('sp_catalogs', 'P') is not null
  8251.     drop proc sp_catalogs
  8252. go
  8253. raiserror(15339,-1,-1,'sp_catalogs')
  8254. go
  8255. create procedure sp_catalogs(
  8256.     @server_name        sysname)
  8257. as
  8258.     select
  8259.         CATALOG_NAME,
  8260.         convert (nvarchar(255),DESCRIPTION)
  8261.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  8262.     order by CATALOG_NAME
  8263. go
  8264. grant execute on sp_catalogs to public
  8265. go
  8266.  
  8267.  
  8268. if object_id('sp_tables_ex', 'P') is not null
  8269.     drop proc sp_tables_ex
  8270. go
  8271. raiserror(15339,-1,-1,'sp_tables_ex')
  8272. go
  8273. create procedure sp_tables_ex(
  8274.     @table_server        sysname,
  8275.     @table_name            sysname = null,
  8276.     @table_schema        sysname = null,
  8277.     @table_catalog        sysname = null,
  8278.     @table_type            sysname = null)
  8279. as
  8280.     declare @table_catalog_param        sysname
  8281.     if ((isnull(charindex('%', @table_catalog),0) = 0) and
  8282.         (isnull(charindex('[', @table_catalog),0) = 0) and
  8283.         (isnull(charindex('_', @table_catalog),0) = 0))
  8284.         select @table_catalog_param = @table_catalog
  8285.     else
  8286.         select @table_catalog_param = null
  8287.  
  8288.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8289.         (isnull(charindex('[', @table_name),0) = 0) and
  8290.         (isnull(charindex('_', @table_name),0) = 0))
  8291.     begin    /*    If no wild carding */
  8292.         select
  8293.             TABLE_CAT = TABLE_CATALOG,
  8294.             TABLE_SCHEM = TABLE_SCHEMA,
  8295.             TABLE_NAME = TABLE_NAME,
  8296.             TABLE_TYPE = TABLE_TYPE,
  8297.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8298.         from master.dbo.SYSREMOTE_TABLES <
  8299.                     @table_server,
  8300.                     @table_catalog_param,
  8301.                     NULL,
  8302.                     @table_name,
  8303.                     NULL >
  8304.         where (TABLE_SCHEMA like @table_schema
  8305.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8306.             and (TABLE_CATALOG like @table_catalog
  8307.             or     @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8308.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8309.             or     @table_type is NULL)
  8310.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8311.     end
  8312.     else
  8313.     begin    /*    If wild carding */
  8314.         select
  8315.             TABLE_CAT = TABLE_CATALOG,
  8316.             TABLE_SCHEM = TABLE_SCHEMA,
  8317.             TABLE_NAME = TABLE_NAME,
  8318.             TABLE_TYPE = TABLE_TYPE,
  8319.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8320.         from master.dbo.SYSREMOTE_TABLES <
  8321.                     @table_server,
  8322.                     @table_catalog_param,
  8323.                     NULL,
  8324.                     NULL,
  8325.                     NULL >
  8326.         where (TABLE_SCHEMA like @table_schema
  8327.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8328.             and (TABLE_CATALOG like @table_catalog
  8329.             or     @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8330.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8331.             or     @table_type is NULL)
  8332.             and TABLE_NAME like @table_name
  8333.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8334.     end
  8335. go
  8336. grant execute on sp_tables_ex to public
  8337. go
  8338.  
  8339.  
  8340. if object_id('sp_columns_ex', 'P') is not null
  8341.     drop proc sp_columns_ex
  8342. go
  8343.  
  8344. /* 7.0 version */
  8345. raiserror(15339,-1,-1,'sp_columns_ex')
  8346. go
  8347.  
  8348. create procedure sp_columns_ex(
  8349.     @table_server        sysname,
  8350.     @table_name            sysname = null,
  8351.     @table_schema        sysname = null,
  8352.     @table_catalog        sysname = null,
  8353.     @column_name        sysname = null,
  8354.     @ODBCVer            int = 2)
  8355. as
  8356.  
  8357.     set nocount on
  8358.     declare
  8359.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  8360.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  8361.  
  8362.     select
  8363.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  8364.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  8365.  
  8366.     declare    
  8367.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8368.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8369.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8370.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  8371.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8372.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  8373.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8374.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  8375.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  8376.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  8377.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  8378.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8379.     @DBTYPE_DBTIMESTAMP smallint
  8380.  
  8381.     select
  8382.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  8383.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  8384.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  8385.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8386.     @DBTYPE_VARNUMERIC    = 139,    @DBTYPE_DECIMAL        = 14,
  8387.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  8388.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  8389.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  8390.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  8391.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  8392.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  8393.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  8394.     @DBTYPE_DBTIMESTAMP    = 135
  8395.  
  8396.     declare    
  8397.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  8398.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  8399.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  8400.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8401.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8402.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  8403.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  8404.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  8405.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  8406.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  8407.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  8408.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  8409.  
  8410.     select     
  8411.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8412.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  8413.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8414.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8415.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8416.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8417.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  8418.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  8419.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  8420.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  8421.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  8422.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  8423.  
  8424.     declare 
  8425.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8426.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8427.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  8428.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  8429.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  8430.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8431.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  8432.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  8433.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  8434.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8435.     @ODBC_DATETIME smallint
  8436.  
  8437.     select
  8438.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  8439.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  8440.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  8441.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  8442.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  8443.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  8444.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  8445.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  8446.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  8447.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  8448.     @ODBC_DATETIME    =    
  8449.     case @ODBCVer
  8450.         when 2 then 11
  8451.         else 93
  8452.     end
  8453.  
  8454.     create table #tmp_columns
  8455.     (    TABLE_CAT sysname  NULL,
  8456.         TABLE_SCHEM sysname  NULL,
  8457.         TABLE_NAME sysname NOT NULL,
  8458.         COLUMN_NAME sysname  NULL,
  8459.         DATA_TYPE smallint NOT NULL,
  8460.         TYPE_NAME sysname   NULL,
  8461.         COLUMN_SIZE int NULL,
  8462.         BUFFER_LENGTH int NULL,
  8463.         DECIMAL_DIGITS smallint NULL,
  8464.         NUM_PREC_RADIX smallint NULL,
  8465.         NULLABLE smallint NOT NULL,
  8466.         REMARKS nvarchar(255)  NULL,
  8467.             COLUMN_DEF nvarchar(128)  NULL,
  8468.             SQL_DATA_TYPE smallint null,
  8469.             SQL_DATETIME_SUB smallint NULL,
  8470.         CHAR_OCTET_LENGTH int NULL,
  8471.         ORDINAL_POSITION smallint,
  8472.         IS_NULLABLE varchar(254)  NOT NULL,
  8473.         SS_DATA_TYPE tinyint null,
  8474.         COLUMN_FLAGS int NOT NULL)
  8475.  
  8476.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8477.         (isnull(charindex('[', @table_name),0) = 0) and
  8478.         (isnull(charindex('_', @table_name),0) = 0))
  8479.     begin    /*    If no wild carding */
  8480.         insert into #tmp_columns
  8481.         select
  8482.             TABLE_CAT = c.TABLE_CATALOG,
  8483.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8484.             TABLE_NAME = c.TABLE_NAME,
  8485.             COLUMN_NAME = c.COLUMN_NAME,
  8486.             DATA_TYPE =
  8487.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8488.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  8489.                 when @DBTYPE_I4                then @ODBC_INT
  8490.                 when @DBTYPE_R4                then @ODBC_REAL
  8491.                 when @DBTYPE_R8                then @ODBC_FLOAT
  8492.                 when @DBTYPE_CY                then @ODBC_MONEY
  8493.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  8494.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  8495.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  8496.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8497.                 when @DBTYPE_BOOL            then @ODBC_BIT
  8498.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8499.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8500.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  8501.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8502.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8503.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  8504.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8505.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8506.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  8507.                 when @DBTYPE_GUID            then @ODBC_GUID
  8508.                 when @DBTYPE_BYTES then
  8509.                     case
  8510.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8511.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8512.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8513.                                  p.COLUMN_SIZE = 8
  8514.                             then @ODBC_TIMESTAMP
  8515.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8516.                             then @ODBC_BINARY
  8517.                         else @ODBC_VARBINARY
  8518.                     end
  8519.                 when @DBTYPE_STR then
  8520.                     case
  8521.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8522.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8523.                         else @ODBC_VARCHAR
  8524.                     end
  8525.                 when @DBTYPE_WSTR then
  8526.                     case
  8527.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8528.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8529.                         else @ODBC_NVARCHAR
  8530.                     end
  8531.                 when @DBTYPE_BSTR then
  8532.                     case
  8533.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8534.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8535.                         else @ODBC_NVARCHAR
  8536.                     end
  8537.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  8538.             end,
  8539.             TYPE_NAME = p.TYPE_NAME,
  8540.             COLUMN_SIZE =
  8541.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8542.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8543.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8544.             NUM_PREC_RADIX = NULL,
  8545.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8546.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8547.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8548.             SQL_DATA_TYPE = NULL,
  8549.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8550.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8551.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8552.             IS_NULLABLE =
  8553.             case c.IS_NULLABLE
  8554.                 when 1 then 'YES'
  8555.                 else 'NO'
  8556.             end,
  8557.             SS_DATA_TYPE = NULL,
  8558.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8559.         from master.dbo.SYSREMOTE_COLUMNS <
  8560.                     @table_server,
  8561.                     @table_catalog,
  8562.                     NULL,
  8563.                     @table_name,
  8564.                     NULL > c,
  8565.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  8566.                     @table_server > p
  8567.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8568.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8569.                 (TABLE_SCHEMA like @table_schema
  8570.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8571.                 and (c.COLUMN_NAME like @column_name
  8572.                 or     @column_name is NULL)
  8573.     end
  8574.     else
  8575.     begin    /*    If wild carding */
  8576.         insert into #tmp_columns
  8577.         select
  8578.             TABLE_CAT = c.TABLE_CATALOG,
  8579.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8580.             TABLE_NAME = c.TABLE_NAME,
  8581.             COLUMN_NAME = c.COLUMN_NAME,
  8582.             DATA_TYPE =
  8583.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8584.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  8585.                 when @DBTYPE_I4                then @ODBC_INT
  8586.                 when @DBTYPE_R4                then @ODBC_REAL
  8587.                 when @DBTYPE_R8                then @ODBC_FLOAT
  8588.                 when @DBTYPE_CY                then @ODBC_MONEY
  8589.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  8590.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  8591.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  8592.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8593.                 when @DBTYPE_BOOL            then @ODBC_BIT
  8594.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8595.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8596.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  8597.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8598.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8599.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  8600.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8601.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8602.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  8603.                 when @DBTYPE_GUID            then @ODBC_GUID
  8604.                 when @DBTYPE_BYTES then
  8605.                     case
  8606.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8607.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8608.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8609.                                  p.COLUMN_SIZE = 8
  8610.                             then @ODBC_TIMESTAMP
  8611.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8612.                             then @ODBC_BINARY
  8613.                         else @ODBC_VARBINARY
  8614.                     end
  8615.                 when @DBTYPE_STR then
  8616.                     case
  8617.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8618.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8619.                         else @ODBC_VARCHAR
  8620.                     end
  8621.                 when @DBTYPE_WSTR then
  8622.                     case
  8623.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8624.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8625.                         else @ODBC_NVARCHAR
  8626.                     end
  8627.                 when @DBTYPE_BSTR then
  8628.                     case
  8629.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8630.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8631.                         else @ODBC_NVARCHAR
  8632.                     end
  8633.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  8634.             end,
  8635.             TYPE_NAME = p.TYPE_NAME,
  8636.             COLUMN_SIZE =
  8637.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8638.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8639.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8640.             NUM_PREC_RADIX = NULL,
  8641.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8642.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8643.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8644.             SQL_DATA_TYPE = NULL,
  8645.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8646.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8647.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8648.             IS_NULLABLE =
  8649.             case c.IS_NULLABLE
  8650.                 when 1 then 'YES'
  8651.                 else 'NO'
  8652.             end,
  8653.             SS_DATA_TYPE = NULL,
  8654.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8655.         from master.dbo.SYSREMOTE_COLUMNS <
  8656.                     @table_server,
  8657.                     @table_catalog,
  8658.                     NULL,
  8659.                     NULL,
  8660.                     NULL > c,
  8661.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  8662.                     @table_server > p
  8663.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8664.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8665.                 (TABLE_SCHEMA like @table_schema
  8666.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8667.                 and (c.COLUMN_NAME like @column_name
  8668.                 or     @column_name is NULL)
  8669.                 and c.TABLE_NAME like @table_name
  8670.     end
  8671.  
  8672.     update #tmp_columns
  8673.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  8674.         SQL_DATETIME_SUB =
  8675.         case
  8676.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  8677.             else #tmp_columns.SQL_DATETIME_SUB
  8678.         end,
  8679.         TYPE_NAME = 
  8680.         case 
  8681.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8682.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8683.                     BUFFER_LENGTH = 8
  8684.                 then 'timestamp'
  8685.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8686.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8687.                 then 'smalldatetime'
  8688.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  8689.                     #tmp_columns.COLUMN_SIZE = 10
  8690.                 then 'smallmoney'
  8691.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  8692.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  8693.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  8694.                 then spt_dt.TYPE_NAME
  8695.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME)
  8696.         end,
  8697.         COLUMN_SIZE =
  8698.         case
  8699.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  8700.                 spt_dt.data_precision
  8701.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8702.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8703.                 then 16
  8704.             else #tmp_columns.COLUMN_SIZE
  8705.         end,
  8706.         BUFFER_LENGTH =
  8707.         case
  8708.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  8709.                 COLUMN_SIZE+2
  8710.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  8711.             else #tmp_columns.BUFFER_LENGTH
  8712.         end,
  8713.         DECIMAL_DIGITS =
  8714.         case
  8715.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  8716.                 then #tmp_columns.SQL_DATETIME_SUB
  8717.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  8718.         end,
  8719.         NUM_PREC_RADIX = spt_dt.RADIX,
  8720.         SS_DATA_TYPE = 
  8721.         case
  8722.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  8723.                 and #tmp_columns.NULLABLE = 1
  8724.                     then @ODS_INT_NULL
  8725.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  8726.                 then @ODS_TINYINT_NOT_NULL
  8727.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  8728.                 then @ODS_SMALLINT_NOT_NULL
  8729.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  8730.                 then @ODS_INT_NOT_NULL
  8731.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  8732.                 then @ODS_FLOAT_NULL
  8733.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  8734.                 then @ODS_REAL_NOT_NULL
  8735.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  8736.                 then @ODS_FLOAT_NOT_NULL
  8737.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  8738.                 then @ODS_MONEY_NULL
  8739.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  8740.                 then @ODS_MONEY_NOT_NULL
  8741.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  8742.                 then @ODS_DATETIME_NULL
  8743.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  8744.                 then @ODS_DATETIME_NOT_NULL
  8745.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  8746.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  8747.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  8748.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  8749.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  8750.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  8751.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  8752.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8753.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8754.                     BUFFER_LENGTH = 8
  8755.                 then @ODS_TIMESTAMP
  8756.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  8757.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8758.                 then  @ODS_BINARY_NULL
  8759.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  8760.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8761.                 then @ODS_BINARY_NOT_NULL
  8762.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  8763.                 then @ODS_VARBINARY    
  8764.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  8765.                 then @ODS_CHAR_NULL
  8766.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  8767.                 then @ODS_CHAR_NOT_NULL
  8768.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  8769.             else null
  8770.         end
  8771.         from master.dbo.spt_datatype_info spt_dt
  8772.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  8773.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  8774.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  8775.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  8776.  
  8777.     select
  8778.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  8779.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  8780.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  8781.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  8782.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  8783.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  8784.             SS_DATA_TYPE
  8785.         from #tmp_columns
  8786.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  8787.  
  8788.         drop table #tmp_columns
  8789.  
  8790. go
  8791. if (charindex('8.00', @@version) > 0)
  8792.     drop procedure sp_columns_ex
  8793. else
  8794. begin
  8795.     print ''
  8796.     print ''
  8797.     print 'Warning:'
  8798.     print 'you are installing the stored procedures '
  8799.     print 'on a pre 8.0 SQL Server.'
  8800.     print 'Ignore the following errors.'
  8801. end
  8802. go
  8803.  
  8804. /* 8.0 version */
  8805. raiserror(15339,-1,-1,'sp_columns_ex')
  8806. go
  8807. create procedure sp_columns_ex(
  8808.     @table_server        sysname,
  8809.     @table_name            sysname = null,
  8810.     @table_schema        sysname = null,
  8811.     @table_catalog        sysname = null,
  8812.     @column_name        sysname = null,
  8813.     @ODBCVer            int = 2)
  8814. as
  8815.  
  8816.     set nocount on
  8817.     declare
  8818.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  8819.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  8820.  
  8821.     select
  8822.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  8823.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  8824.  
  8825.     declare    
  8826.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8827.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8828.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8829.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  8830.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8831.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  8832.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8833.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  8834.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  8835.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  8836.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  8837.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8838.     @DBTYPE_DBTIMESTAMP smallint
  8839.  
  8840.     select
  8841.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  8842.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  8843.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  8844.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8845.     @DBTYPE_VARNUMERIC    = 139,    @DBTYPE_DECIMAL        = 14,
  8846.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  8847.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  8848.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  8849.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  8850.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  8851.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  8852.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  8853.     @DBTYPE_DBTIMESTAMP    = 135
  8854.  
  8855.     declare    
  8856.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  8857.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  8858.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  8859.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8860.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8861.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  8862.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  8863.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  8864.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  8865.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  8866.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  8867.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  8868.  
  8869.     select     
  8870.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8871.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  8872.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8873.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8874.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8875.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8876.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  8877.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  8878.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  8879.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  8880.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  8881.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  8882.  
  8883.     declare 
  8884.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8885.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8886.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  8887.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  8888.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  8889.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8890.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  8891.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  8892.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  8893.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8894.     @ODBC_DATETIME smallint
  8895.  
  8896.     select
  8897.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  8898.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  8899.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  8900.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  8901.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  8902.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  8903.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  8904.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  8905.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  8906.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  8907.     @ODBC_DATETIME    =    
  8908.     case @ODBCVer
  8909.         when 2 then 11
  8910.         else 93
  8911.     end
  8912.  
  8913.     create table #tmp_columns
  8914.     (    TABLE_CAT sysname collate database_default NULL,
  8915.         TABLE_SCHEM sysname collate database_default NULL,
  8916.         TABLE_NAME sysname    collate database_default NOT NULL,
  8917.         COLUMN_NAME sysname collate database_default NULL,
  8918.         DATA_TYPE smallint NOT NULL,
  8919.         TYPE_NAME sysname  collate database_default NULL,
  8920.         COLUMN_SIZE int NULL,
  8921.         BUFFER_LENGTH int NULL,
  8922.         DECIMAL_DIGITS smallint NULL,
  8923.         NUM_PREC_RADIX smallint NULL,
  8924.         NULLABLE smallint NOT NULL,
  8925.         REMARKS nvarchar(255) collate database_default NULL,
  8926.             COLUMN_DEF nvarchar(128) collate database_default NULL,
  8927.             SQL_DATA_TYPE smallint null,
  8928.             SQL_DATETIME_SUB smallint NULL,
  8929.         CHAR_OCTET_LENGTH int NULL,
  8930.         ORDINAL_POSITION smallint,
  8931.         IS_NULLABLE varchar(254) collate database_default NOT NULL,
  8932.         SS_DATA_TYPE tinyint null,
  8933.         COLUMN_FLAGS int NOT NULL)
  8934.  
  8935.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8936.         (isnull(charindex('[', @table_name),0) = 0) and
  8937.         (isnull(charindex('_', @table_name),0) = 0))
  8938.     begin    /*    If no wild carding */
  8939.         insert into #tmp_columns
  8940.         select
  8941.             TABLE_CAT = c.TABLE_CATALOG,
  8942.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8943.             TABLE_NAME = c.TABLE_NAME,
  8944.             COLUMN_NAME = c.COLUMN_NAME,
  8945.             DATA_TYPE =
  8946.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8947.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  8948.                 when @DBTYPE_I4                then @ODBC_INT
  8949.                 when @DBTYPE_R4                then @ODBC_REAL
  8950.                 when @DBTYPE_R8                then @ODBC_FLOAT
  8951.                 when @DBTYPE_CY                then @ODBC_MONEY
  8952.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  8953.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  8954.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  8955.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8956.                 when @DBTYPE_BOOL            then @ODBC_BIT
  8957.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8958.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8959.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  8960.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8961.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8962.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  8963.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8964.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8965.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  8966.                 when @DBTYPE_GUID            then @ODBC_GUID
  8967.                 when @DBTYPE_BYTES then
  8968.                     case
  8969.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8970.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8971.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8972.                                  p.COLUMN_SIZE = 8
  8973.                             then @ODBC_TIMESTAMP
  8974.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8975.                             then @ODBC_BINARY
  8976.                         else @ODBC_VARBINARY
  8977.                     end
  8978.                 when @DBTYPE_STR then
  8979.                     case
  8980.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8981.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8982.                         else @ODBC_VARCHAR
  8983.                     end
  8984.                 when @DBTYPE_WSTR then
  8985.                     case
  8986.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8987.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8988.                         else @ODBC_NVARCHAR
  8989.                     end
  8990.                 when @DBTYPE_BSTR then
  8991.                     case
  8992.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8993.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8994.                         else @ODBC_NVARCHAR
  8995.                     end
  8996.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  8997.             end,
  8998.             TYPE_NAME = p.TYPE_NAME,
  8999.             COLUMN_SIZE =
  9000.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9001.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9002.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9003.             NUM_PREC_RADIX = NULL,
  9004.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9005.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9006.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9007.             SQL_DATA_TYPE = NULL,
  9008.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9009.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9010.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9011.             IS_NULLABLE =
  9012.             case c.IS_NULLABLE
  9013.                 when 1 then 'YES'
  9014.                 else 'NO'
  9015.             end,
  9016.             SS_DATA_TYPE = NULL,
  9017.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9018.         from master.dbo.SYSREMOTE_COLUMNS <
  9019.                     @table_server,
  9020.                     @table_catalog,
  9021.                     NULL,
  9022.                     @table_name,
  9023.                     NULL > c,
  9024.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  9025.                     @table_server > p
  9026.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9027.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9028.                 (TABLE_SCHEMA like @table_schema
  9029.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9030.                 and (c.COLUMN_NAME like @column_name
  9031.                 or     @column_name is NULL)
  9032.     end
  9033.     else
  9034.     begin    /*    If wild carding */
  9035.         insert into #tmp_columns
  9036.         select
  9037.             TABLE_CAT = c.TABLE_CATALOG,
  9038.             TABLE_SCHEM = c.TABLE_SCHEMA,
  9039.             TABLE_NAME = c.TABLE_NAME,
  9040.             COLUMN_NAME = c.COLUMN_NAME,
  9041.             DATA_TYPE =
  9042.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  9043.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  9044.                 when @DBTYPE_I4                then @ODBC_INT
  9045.                 when @DBTYPE_R4                then @ODBC_REAL
  9046.                 when @DBTYPE_R8                then @ODBC_FLOAT
  9047.                 when @DBTYPE_CY                then @ODBC_MONEY
  9048.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  9049.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  9050.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  9051.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  9052.                 when @DBTYPE_BOOL            then @ODBC_BIT
  9053.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  9054.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  9055.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  9056.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  9057.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  9058.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  9059.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  9060.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  9061.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  9062.                 when @DBTYPE_GUID            then @ODBC_GUID
  9063.                 when @DBTYPE_BYTES then
  9064.                     case
  9065.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  9066.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9067.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9068.                                  p.COLUMN_SIZE = 8
  9069.                             then @ODBC_TIMESTAMP
  9070.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9071.                             then @ODBC_BINARY
  9072.                         else @ODBC_VARBINARY
  9073.                     end
  9074.                 when @DBTYPE_STR then
  9075.                     case
  9076.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  9077.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  9078.                         else @ODBC_VARCHAR
  9079.                     end
  9080.                 when @DBTYPE_WSTR then
  9081.                     case
  9082.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9083.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9084.                         else @ODBC_NVARCHAR
  9085.                     end
  9086.                 when @DBTYPE_BSTR then
  9087.                     case
  9088.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9089.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9090.                         else @ODBC_NVARCHAR
  9091.                     end
  9092.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  9093.             end,
  9094.             TYPE_NAME = p.TYPE_NAME,
  9095.             COLUMN_SIZE =
  9096.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9097.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9098.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9099.             NUM_PREC_RADIX = NULL,
  9100.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9101.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9102.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9103.             SQL_DATA_TYPE = NULL,
  9104.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9105.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9106.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9107.             IS_NULLABLE =
  9108.             case c.IS_NULLABLE
  9109.                 when 1 then 'YES'
  9110.                 else 'NO'
  9111.             end,
  9112.             SS_DATA_TYPE = NULL,
  9113.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9114.         from master.dbo.SYSREMOTE_COLUMNS <
  9115.                     @table_server,
  9116.                     @table_catalog,
  9117.                     NULL,
  9118.                     NULL,
  9119.                     NULL > c,
  9120.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  9121.                     @table_server > p
  9122.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9123.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9124.                 (TABLE_SCHEMA like @table_schema
  9125.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9126.                 and (c.COLUMN_NAME like @column_name
  9127.                 or     @column_name is NULL)
  9128.                 and c.TABLE_NAME like @table_name
  9129.     end
  9130.  
  9131.     update #tmp_columns
  9132.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  9133.         SQL_DATETIME_SUB =
  9134.         case
  9135.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  9136.             else #tmp_columns.SQL_DATETIME_SUB
  9137.         end,
  9138.         TYPE_NAME = 
  9139.         case 
  9140.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9141.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9142.                     BUFFER_LENGTH = 8
  9143.                 then 'timestamp'
  9144.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9145.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9146.                 then 'smalldatetime'
  9147.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  9148.                     #tmp_columns.COLUMN_SIZE = 10
  9149.                 then 'smallmoney'
  9150.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  9151.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  9152.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  9153.                 then spt_dt.TYPE_NAME collate database_default
  9154.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME collate database_default)
  9155.         end,
  9156.         COLUMN_SIZE =
  9157.         case
  9158.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  9159.                 spt_dt.data_precision
  9160.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9161.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9162.                 then 16
  9163.             else #tmp_columns.COLUMN_SIZE
  9164.         end,
  9165.         BUFFER_LENGTH =
  9166.         case
  9167.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  9168.                 COLUMN_SIZE+2
  9169.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  9170.             else #tmp_columns.BUFFER_LENGTH
  9171.         end,
  9172.         DECIMAL_DIGITS =
  9173.         case
  9174.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  9175.                 then #tmp_columns.SQL_DATETIME_SUB
  9176.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  9177.         end,
  9178.         NUM_PREC_RADIX = spt_dt.RADIX,
  9179.         SS_DATA_TYPE = 
  9180.         case
  9181.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  9182.                 and #tmp_columns.NULLABLE = 1
  9183.                     then @ODS_INT_NULL
  9184.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  9185.                 then @ODS_TINYINT_NOT_NULL
  9186.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  9187.                 then @ODS_SMALLINT_NOT_NULL
  9188.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  9189.                 then @ODS_INT_NOT_NULL
  9190.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  9191.                 then @ODS_FLOAT_NULL
  9192.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  9193.                 then @ODS_REAL_NOT_NULL
  9194.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  9195.                 then @ODS_FLOAT_NOT_NULL
  9196.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  9197.                 then @ODS_MONEY_NULL
  9198.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  9199.                 then @ODS_MONEY_NOT_NULL
  9200.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  9201.                 then @ODS_DATETIME_NULL
  9202.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  9203.                 then @ODS_DATETIME_NOT_NULL
  9204.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  9205.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  9206.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  9207.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  9208.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  9209.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  9210.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  9211.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9212.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9213.                     BUFFER_LENGTH = 8
  9214.                 then @ODS_TIMESTAMP
  9215.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  9216.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9217.                 then  @ODS_BINARY_NULL
  9218.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  9219.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9220.                 then @ODS_BINARY_NOT_NULL
  9221.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  9222.                 then @ODS_VARBINARY    
  9223.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  9224.                 then @ODS_CHAR_NULL
  9225.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  9226.                 then @ODS_CHAR_NOT_NULL
  9227.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  9228.             else null
  9229.         end
  9230.         from master.dbo.spt_datatype_info spt_dt
  9231.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  9232.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  9233.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  9234.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  9235.  
  9236.     select
  9237.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  9238.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  9239.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  9240.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  9241.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  9242.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  9243.             SS_DATA_TYPE
  9244.         from #tmp_columns
  9245.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  9246.  
  9247.         drop table #tmp_columns
  9248.  
  9249. go
  9250. grant execute on sp_columns_ex to public
  9251. go
  9252.  
  9253.  
  9254. if object_id('sp_table_privileges_ex', 'P') is not null
  9255.     drop proc sp_table_privileges_ex
  9256. go
  9257. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  9258. go
  9259. create procedure sp_table_privileges_ex(
  9260.     @table_server        sysname,
  9261.     @table_name            sysname = null,
  9262.     @table_schema        sysname = null,
  9263.     @table_catalog        sysname = null)
  9264. as
  9265.     select
  9266.         TABLE_CAT = TABLE_CATALOG,
  9267.         TABLE_SCHEM = TABLE_SCHEMA,
  9268.         TABLE_NAME = TABLE_NAME,
  9269.         GRANTOR = GRANTOR,
  9270.         GRANTEE = GRANTEE,
  9271.         PRIVILEGE = PRIVILEGE_TYPE,
  9272.         IS_GRANTABLE = 
  9273.         case IS_GRANTABLE
  9274.             when 1 then 'YES'
  9275.             when 0 then 'NO'
  9276.             else null
  9277.         end
  9278.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  9279.                 @table_server,
  9280.                 @table_catalog,
  9281.                 NULL,
  9282.                 NULL >
  9283.     where (TABLE_SCHEMA like @table_schema
  9284.         or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9285.         and (TABLE_NAME like @table_name
  9286.         or     @table_name is NULL)
  9287.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  9288. go
  9289. grant execute on sp_table_privileges_ex to public
  9290. go
  9291.  
  9292.  
  9293. if object_id('sp_column_privileges_ex', 'P') is not null
  9294.     drop proc sp_column_privileges_ex
  9295. go
  9296. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  9297. go
  9298. create procedure sp_column_privileges_ex(
  9299.     @table_server        sysname,
  9300.     @table_name            sysname = null,
  9301.     @table_schema        sysname = null,
  9302.     @table_catalog        sysname = null,
  9303.     @column_name        sysname = null)
  9304. as
  9305.     select
  9306.         TABLE_CAT = TABLE_CATALOG,
  9307.         TABLE_SCHEM = TABLE_SCHEMA,
  9308.         TABLE_NAME = TABLE_NAME,
  9309.         COLUMN_NAME = COLUMN_NAME,
  9310.         GRANTOR = GRANTOR,
  9311.         GRANTEE = GRANTEE,
  9312.         PRIVILEGE = PRIVILEGE_TYPE,
  9313.         IS_GRANTABLE = 
  9314.         case IS_GRANTABLE
  9315.             when 1 then 'YES'
  9316.             when 0 then 'NO'
  9317.             else null
  9318.         end
  9319.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  9320.         @table_server,
  9321.         @table_catalog,
  9322.         @table_schema,
  9323.         @table_name,
  9324.         NULL >
  9325.     where (COLUMN_NAME like @column_name
  9326.         or @column_name is NULL)
  9327.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  9328. go
  9329. grant execute on sp_column_privileges_ex to public
  9330. go
  9331.  
  9332.  
  9333. if object_id('sp_indexes', 'P') is not null
  9334.     drop proc sp_indexes
  9335. go
  9336. raiserror(15339,-1,-1,'sp_indexes')
  9337. go
  9338. create procedure sp_indexes(
  9339.     @table_server        sysname,
  9340.     @table_name            sysname = null,
  9341.     @table_schema        sysname = null,
  9342.     @table_catalog        sysname = null,
  9343.     @index_name            sysname = null,
  9344.     @is_unique          bit = null)
  9345. as
  9346.     select
  9347.         TABLE_CAT = TABLE_CATALOG,
  9348.         TABLE_SCHEM = TABLE_SCHEMA,
  9349.         TABLE_NAME = TABLE_NAME,
  9350.             NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  9351.         INDEX_QUALIFIER = TABLE_NAME,
  9352.         INDEX_NAME = INDEX_NAME,
  9353.             TYPE =
  9354.         case [CLUSTERED]
  9355.             when 1 then 1
  9356.             else 3
  9357.         end,
  9358.         ORDINAL_POSITION = ORDINAL_POSITION,
  9359.             COLUMN_NAME = COLUMN_NAME,
  9360.         ASC_OR_DESC = 
  9361.         case [COLLATION]
  9362.             when 1 then 'A'
  9363.             when 2 then 'D'
  9364.             else null
  9365.         end,
  9366.             CARDINALITY = CARDINALITY,
  9367.         PAGES = 
  9368.         case [CLUSTERED]
  9369.             when 1 then PAGES
  9370.             else NULL
  9371.         end,
  9372.             FILTER_CONDITION = FILTER_CONDITION
  9373.     from master.dbo.SYSREMOTE_INDEXES <
  9374.                 @table_server,
  9375.                 @table_catalog,
  9376.                 @table_schema,
  9377.                         @index_name,
  9378.                 NULL,            /* TYPE (index type) */
  9379.                 @table_name >
  9380.     where @is_unique is null or @is_unique = [UNIQUE]
  9381.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  9382. go
  9383. grant execute on sp_indexes to public
  9384. go
  9385.  
  9386.  
  9387. if object_id('sp_foreignkeys', 'P') is not null
  9388.     drop proc sp_foreignkeys
  9389. go
  9390. raiserror(15339,-1,-1,'sp_foreignkeys')
  9391. go
  9392. create procedure sp_foreignkeys(
  9393.     @table_server        sysname,
  9394.     @pktab_name         sysname = null,
  9395.     @pktab_schema       sysname = null,
  9396.     @pktab_catalog      sysname = null,
  9397.     @fktab_name         sysname = null,
  9398.     @fktab_schema       sysname = null,
  9399.     @fktab_catalog      sysname = null)
  9400. as
  9401.     select
  9402.         PKTABLE_CAT = PK_TABLE_CATALOG,
  9403.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  9404.         PKTABLE_NAME = PK_TABLE_NAME,
  9405.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  9406.         FKTABLE_CAT = FK_TABLE_CATALOG,
  9407.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  9408.         FKTABLE_NAME = FK_TABLE_NAME,
  9409.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  9410.         KEY_SEQ = ORDINAL,
  9411.         UPDATE_RULE = 
  9412.         case UPDATE_RULE
  9413.             when 'CASCADE' then 0
  9414.             when 'NO ACTION' then 1
  9415.             when 'SET NULL' then 2
  9416.             else null
  9417.         end,
  9418.         DELETE_RULE = 
  9419.         case DELETE_RULE
  9420.             when 'CASCADE' then 0
  9421.             when 'NO ACTION' then 1
  9422.             when 'SET NULL' then 2
  9423.             else null
  9424.         end,
  9425.         FK_NAME = convert(sysname, NULL),
  9426.         PK_NAME = convert(sysname, NULL),
  9427.         DEFERRABILITY = convert(smallint, null)
  9428.  
  9429.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  9430.                 @table_server,
  9431.                 @pktab_catalog,
  9432.                 @pktab_schema,
  9433.                 @pktab_name,
  9434.                 @fktab_catalog,
  9435.                 @fktab_schema,
  9436.                 @fktab_name >
  9437.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  9438. go
  9439. grant execute on sp_foreignkeys to public
  9440. go
  9441.  
  9442.  
  9443. if object_id('sp_primarykeys', 'P') is not null
  9444.     drop proc sp_primarykeys
  9445. go
  9446. raiserror(15339,-1,-1,'sp_primarykeys')
  9447. go
  9448. create procedure sp_primarykeys(
  9449.     @table_server        sysname,
  9450.     @table_name         sysname = null,
  9451.     @table_schema       sysname = null,
  9452.     @table_catalog      sysname = null)
  9453. as
  9454.     select
  9455.         TABLE_CAT = TABLE_CATALOG,
  9456.         TABLE_SCHEM = TABLE_SCHEMA,
  9457.         TABLE_NAME = TABLE_NAME,
  9458.         COLUMN_NAME = COLUMN_NAME,
  9459.         KEY_SEQ = ORDINAL,
  9460.         PK_NAME = convert(sysname, NULL)
  9461.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  9462.                 @table_server,
  9463.                 @table_catalog,
  9464.                 @table_schema,
  9465.                 @table_name >
  9466.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  9467. go
  9468. grant execute on sp_primarykeys to public
  9469. go
  9470.  
  9471. /*-------------- END CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  9472.  
  9473.  
  9474. dump tran master with no_log
  9475. go
  9476.  
  9477. if (charindex('6.50', @@version) = 0 and
  9478.     charindex('7.00', @@version) = 0 and
  9479.     charindex('8.00', @@version) = 0)
  9480. begin
  9481.     print ''
  9482.     print ''
  9483.     print 'Warning:'
  9484.     print 'you are installing the stored procedures '
  9485.     print 'on a pre 6.50 SQL Server.'
  9486.     print 'Ignore the following errors.'
  9487. end
  9488.  
  9489. print 'creating sp_ddopen'
  9490. go
  9491.  
  9492. /*    Procedure for pre-6.50 server */
  9493. create procedure sp_ddopen(
  9494.                @handle            int output,
  9495.                @procname        sysname,
  9496.                @scrollopt        int output,
  9497.                @ccopt            int output,
  9498.                @rows            int output,
  9499.                @p1                varchar(255) = null,
  9500.                @p2                varchar(255) = null,
  9501.                @p3                varchar(255) = null,
  9502.                @p4                varchar(255) = null,
  9503.                @p5                varchar(255) = null,
  9504.                @p6                varchar(255) = null,
  9505.                @p7                int = null,
  9506.                @ODBCVer         int = 2)
  9507. as
  9508.     set nocount on
  9509.     declare @ret int
  9510.  
  9511.     if @procname = 'sp_column_privileges'
  9512.     begin
  9513.         create table #spcolpriv (
  9514.             TABLE_QUALIFIER varchar(32) null,
  9515.             TABLE_OWNER varchar(32) null,
  9516.             TABLE_NAME varchar(32)    not null,
  9517.             COLUMN_NAME varchar(32) not null,
  9518.             GRANTOR varchar(32) null,
  9519.             GRANTEE varchar(32) not null,
  9520.             PRIVILEGE varchar(32) not null,
  9521.             IS_GRANTABLE varchar(3) null
  9522.             )
  9523.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  9524.         exec @ret = sp_cursoropen @handle output,
  9525.             'select * from #spcolpriv',
  9526.             @scrollopt output, @ccopt output, @rows output
  9527.         drop table #spcolpriv
  9528.     end
  9529.     else if @procname = 'sp_columns'
  9530.     begin
  9531.         create table #spcolumns (
  9532.             TABLE_QUALIFIER varchar(32) null,
  9533.             TABLE_OWNER varchar(32) null,
  9534.             TABLE_NAME varchar(32)    not null,
  9535.             COLUMN_NAME varchar(32) not null,
  9536.             DATA_TYPE smallint not null,
  9537.             TYPE_NAME varchar(32) not null,
  9538.             "PRECISION" int null,
  9539.             LENGTH int null,
  9540.             SCALE smallint null,
  9541.             RADIX smallint null,
  9542.             NULLABLE smallint not null,
  9543.             REMARKS varchar(254) null,
  9544.             COLUMN_DEF varchar(255) null,
  9545.             SQL_DATA_TYPE smallint not null,
  9546.             SQL_DATETIME_SUB smallint null,
  9547.             CHAR_OCTET_LENGTH int null,
  9548.             ORDINAL_POSITION int not null,
  9549.             IS_NULLABLE varchar(254) null,
  9550.             SS_DATA_TYPE tinyint null
  9551.             )
  9552.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  9553.         exec @ret = sp_cursoropen @handle output,
  9554.             'select * from #spcolumns',
  9555.             @scrollopt output, @ccopt output, @rows output
  9556.         drop table #spcolumns
  9557.     end
  9558.     else if @procname = 'sp_datatype_info'
  9559.     begin
  9560.         create table #spdatatypeinfo (
  9561.             TYPE_NAME            varchar(32)  not null,
  9562.             DATA_TYPE            smallint not null,
  9563.             "PRECISION"            int null,
  9564.             LITERAL_PREFIX        varchar(32)    null,
  9565.             LITERAL_SUFFIX        varchar(32)    null,
  9566.             CREATE_PARAMS        varchar(32)    null,
  9567.             NULLABLE            smallint   not null,
  9568.             CASE_SENSITIVE        smallint   not null,
  9569.             SEARCHABLE            smallint   not null,
  9570.             UNSIGNED_ATTRIBUTE    smallint   null,
  9571.             MONEY    smallint    not null,
  9572.             AUTO_INCREMENT        smallint    null,
  9573.             LOCAL_TYPE_NAME     varchar(32) null,
  9574.             MINIMUM_SCALE        smallint     null,
  9575.             MAXIMUM_SCALE        smallint   null,
  9576.             SQL_DATA_TYPE        smallint      not null,
  9577.             SQL_DATETIME_SUB    smallint   null,
  9578.             NUM_PREC_RADIX        int     null,
  9579.             INTERVAL_PRECISION    smallint    NULL,
  9580.             USERTYPE            smallint not null)
  9581.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  9582.         exec @ret = sp_cursoropen @handle output,
  9583.             'select * from #spdatatypeinfo',
  9584.             @scrollopt output, @ccopt output, @rows output
  9585.         drop table #spdatatypeinfo
  9586.     end
  9587.     else if @procname = 'sp_fkeys'
  9588.     begin
  9589.         create table #spfkeys (
  9590.             PKTABLE_QUALIFIER varchar(32)     null,
  9591.             PKTABLE_OWNER varchar(32)    null,
  9592.             PKTABLE_NAME varchar(32)  not null,
  9593.             PKCOLUMN_NAME varchar(32)    not null,
  9594.             FKTABLE_QUALIFIER varchar(32)    null,
  9595.             FKTABLE_OWNER varchar(32)    null,
  9596.             FKTABLE_NAME varchar(32)  not null,
  9597.             FKCOLUMN_NAME varchar(32)    not null,
  9598.             KEY_SEQ smallint not null,
  9599.             UPDATE_RULE smallint null,
  9600.             DELETE_RULE smallint null,
  9601.             FK_NAME varchar(32) null,
  9602.             PK_NAME varchar(32) null,
  9603.             DEFERRABILITY smallint null
  9604.             )
  9605.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  9606.         exec @ret = sp_cursoropen @handle output,
  9607.             'select * from #spfkeys',
  9608.             @scrollopt output, @ccopt output, @rows output
  9609.         drop table #spfkeys
  9610.     end
  9611.     else if @procname = 'sp_pkeys'
  9612.     begin
  9613.         create table #sppkeys (
  9614.             TABLE_QUALIFIER varchar(32)   null,
  9615.             TABLE_OWNER varchar(32)   null,
  9616.             TABLE_NAME varchar(32)    not null,
  9617.             COLUMN_NAME varchar(32)  not null,
  9618.             KEY_SEQ smallint not null,
  9619.             PK_NAME varchar(32) null
  9620.             )
  9621.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  9622.         exec @ret = sp_cursoropen @handle output,
  9623.             'select * from #sppkeys',
  9624.             @scrollopt output, @ccopt output, @rows output
  9625.         drop table #sppkeys
  9626.     end
  9627.     else if @procname = 'sp_special_columns'
  9628.     begin
  9629.         create table #spspeccol (
  9630.             SCOPE smallint null,
  9631.             COLUMN_NAME varchar(32) not null,
  9632.             DATA_TYPE smallint not null,
  9633.             TYPE_NAME varchar(32) not null,
  9634.             "PRECISION" int null,
  9635.             LENGTH int null,
  9636.             SCALE smallint null,
  9637.             PSEUDO_COLUMN smallint null
  9638.             )
  9639.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  9640.         exec @ret = sp_cursoropen @handle output,
  9641.             'select * from #spspeccol',
  9642.             @scrollopt output, @ccopt output, @rows output
  9643.         drop table #spspeccol
  9644.     end
  9645.     else if @procname = 'sp_sproc_columns'
  9646.     begin
  9647.         create table #spproccol (
  9648.             PROCEDURE_QUALIFIER varchar(32)  null,
  9649.             PROCEDURE_OWNER varchar(32)  null,
  9650.             PROCEDURE_NAME varchar(32)    not null,
  9651.             COLUMN_NAME varchar(32) not null,
  9652.             COLUMN_TYPE smallint not null,
  9653.             DATA_TYPE smallint not null,
  9654.             TYPE_NAME varchar(32) not null,
  9655.             "PRECISION" int null,
  9656.             LENGTH int null,
  9657.             SCALE smallint null,
  9658.             RADIX smallint null,
  9659.             NULLABLE smallint not null,
  9660.             REMARKS varchar(254) null,
  9661.             COLUMN_DEF varchar(255) null,
  9662.             SQL_DATA_TYPE smallint not null,
  9663.             SQL_DATETIME_SUB smallint null,
  9664.             CHAR_OCTET_LENGTH int null,
  9665.             ORDINAL_POSITION int not null,
  9666.             IS_NULLABLE varchar(254) null,
  9667.             SS_DATA_TYPE tinyint null
  9668.             )
  9669.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  9670.         exec @ret = sp_cursoropen @handle output,
  9671.             'select * from #spproccol',
  9672.             @scrollopt output, @ccopt output, @rows output
  9673.         drop table #spproccol
  9674.     end
  9675.     else if @procname = 'sp_statistics'
  9676.     begin
  9677.         create table #spstatistics (
  9678.             TABLE_QUALIFIER varchar(32)   null,
  9679.             TABLE_OWNER varchar(32)   null,
  9680.             TABLE_NAME varchar(32)    not null,
  9681.             NON_UNIQUE smallint null,
  9682.             INDEX_QUALIFIER varchar(32) null,
  9683.             INDEX_NAME varchar(32)    null,
  9684.             TYPE smallint not null,
  9685.             SEQ_IN_INDEX smallint null,
  9686.             COLUMN_NAME varchar(32) null,
  9687.             COLLATION char(1) null,
  9688.             CARDINALITY int null,
  9689.             PAGES int null,
  9690.             FILTER_CONDITION varchar(128) null
  9691.             )
  9692.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  9693.         exec @ret = sp_cursoropen @handle output,
  9694.             'select * from #spstatistics',
  9695.             @scrollopt output, @ccopt output, @rows output
  9696.         drop table #spstatistics
  9697.     end
  9698.     else if @procname = 'sp_stored_procedures'
  9699.     begin
  9700.         create table #spprocedures (
  9701.             PROCEDURE_QUALIFIER varchar(32)  null,
  9702.             PROCEDURE_OWNER varchar(32)  null,
  9703.             PROCEDURE_NAME varchar(32)    not null,
  9704.             NUM_INPUT_PARAMS int null,
  9705.             NUM_OUTPUT_PARAMS int null,
  9706.             NUM_RESULT_SETS int null,
  9707.             REMARKS varchar(254) null,
  9708.             PROCEDURE_TYPE smallint null
  9709.             )
  9710.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  9711.         exec @ret = sp_cursoropen @handle output,
  9712.             'select * from #spprocedures',
  9713.             @scrollopt output, @ccopt output, @rows output
  9714.         drop table #spprocedures
  9715.     end
  9716.     else if @procname = 'sp_table_privileges'
  9717.     begin
  9718.         create table #sptabpriv (
  9719.             TABLE_QUALIFIER varchar(32) null,
  9720.             TABLE_OWNER varchar(32) null,
  9721.             TABLE_NAME varchar(32)    not null,
  9722.             GRANTOR varchar(32) null,
  9723.             GRANTEE varchar(32) not null,
  9724.             PRIVILEGE varchar(32) not null,
  9725.             IS_GRANTABLE varchar(3) null
  9726.             )
  9727.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  9728.         exec @ret = sp_cursoropen @handle output,
  9729.             'select * from #sptabpriv',
  9730.             @scrollopt output, @ccopt output, @rows output
  9731.         drop table #sptabpriv
  9732.     end
  9733.     else if @procname = 'sp_tables'
  9734.     begin
  9735.         create table #sptables (
  9736.             TABLE_QUALIFIER varchar(32) null,
  9737.             TABLE_OWNER varchar(32) null,
  9738.             TABLE_NAME varchar(32)    null,
  9739.             TABLE_TYPE     varchar(32) null,
  9740.             REMARKS varchar(254) null)
  9741.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  9742.         exec @ret = sp_cursoropen @handle output,
  9743.             'select * from #sptables',
  9744.             @scrollopt output, @ccopt output, @rows output
  9745.         drop table #sptables
  9746.     end
  9747.     else
  9748.         print 'Unknown sp_ddopen procedure'
  9749.     select @ret = isnull(@ret,0)
  9750.     return isnull(@ret,0)
  9751. go
  9752.  
  9753. if (charindex('7.00', @@version) > 0 or
  9754.     charindex('8.00', @@version) > 0)
  9755.     drop procedure sp_ddopen
  9756. else
  9757. begin
  9758.     print ''
  9759.     print ''
  9760.     print 'Warning:'
  9761.     print 'you are installing the stored procedures '
  9762.     print 'on a pre 7.0 SQL Server.'
  9763.     print 'Ignore the following errors.'
  9764. end
  9765. go
  9766.  
  9767. /*    Procedure for 7.0 server */
  9768. create procedure sp_ddopen; 1(
  9769.                @handle            int output,
  9770.                @procname        sysname,
  9771.                @scrollopt        int output,
  9772.                @ccopt            int output,
  9773.                @rows            int output,
  9774.                @p1                nvarchar(774) = null,
  9775.                @p2                nvarchar(774) = null,
  9776.                @p3                nvarchar(774) = null,
  9777.                @p4                nvarchar(774) = null,
  9778.                @p5                nvarchar(774) = null,
  9779.                @p6                nvarchar(774) = null,
  9780.                @p7                int = null,
  9781.                @ODBCVer         int = 2)
  9782. as
  9783.     set nocount on
  9784.     declare @ret int
  9785.  
  9786.     if @procname = 'sp_column_privileges'
  9787.     begin
  9788.         exec @ret = sp_ddopen;2 @handle output, 
  9789.                             @scrollopt output, 
  9790.                             @ccopt output, 
  9791.                             @rows output, 
  9792.                             @p1,
  9793.                             @p2,
  9794.                             @p3,
  9795.                             @p4
  9796.     end
  9797.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  9798.     begin
  9799.         exec @ret = sp_ddopen;3 @handle output,
  9800.                             @procname,
  9801.                             @scrollopt output, 
  9802.                             @ccopt output, 
  9803.                             @rows output, 
  9804.                             @p1,
  9805.                             @p2,
  9806.                             @p3,
  9807.                             @p4,
  9808.                             @p5,
  9809.                             @ODBCVer
  9810.     end
  9811.     else if @procname = 'sp_datatype_info'
  9812.     begin
  9813.         exec @ret = sp_ddopen;4 @handle output,
  9814.                             @scrollopt output, 
  9815.                             @ccopt output, 
  9816.                             @rows output, 
  9817.                             @p7,
  9818.                             @ODBCVer
  9819.     end
  9820.     else if @procname = 'sp_fkeys'
  9821.     begin
  9822.         exec @ret = sp_ddopen;5 @handle output, 
  9823.                             @scrollopt output, 
  9824.                             @ccopt output, 
  9825.                             @rows output, 
  9826.                             @p1,
  9827.                             @p2,
  9828.                             @p3,
  9829.                             @p4,
  9830.                             @p5,
  9831.                             @p6
  9832.     end
  9833.     else if @procname = 'sp_pkeys'
  9834.     begin
  9835.         exec @ret = sp_ddopen;6 @handle output, 
  9836.                             @scrollopt output, 
  9837.                             @ccopt output, 
  9838.                             @rows output, 
  9839.                             @p1,
  9840.                             @p2,
  9841.                             @p3
  9842.     end
  9843.     else if @procname = 'sp_special_columns'
  9844.     begin
  9845.         exec @ret = sp_ddopen;7 @handle output, 
  9846.                             @scrollopt output, 
  9847.                             @ccopt output, 
  9848.                             @rows output, 
  9849.                             @p1,
  9850.                             @p2,
  9851.                             @p3,
  9852.                             @p4,
  9853.                             @p5,
  9854.                             @p6,
  9855.                             @ODBCVer
  9856.     end
  9857.     else if @procname = 'sp_sproc_columns'
  9858.     begin
  9859.         exec @ret = sp_ddopen;8 @handle output, 
  9860.                             @scrollopt output, 
  9861.                             @ccopt output, 
  9862.                             @rows output, 
  9863.                             @p1,
  9864.                             @p2,
  9865.                             @p3,
  9866.                             @p4,
  9867.                             @ODBCVer
  9868.     end
  9869.     else if @procname = 'sp_statistics'
  9870.     begin
  9871.         exec @ret = sp_ddopen;9 @handle output, 
  9872.                             @scrollopt output, 
  9873.                             @ccopt output, 
  9874.                             @rows output, 
  9875.                             @p1,
  9876.                             @p2,
  9877.                             @p3,
  9878.                             @p4,
  9879.                             @p5,
  9880.                             @p6
  9881.     end
  9882.     else if @procname = 'sp_stored_procedures'
  9883.     begin
  9884.         exec @ret = sp_ddopen;10 @handle output, 
  9885.                              @scrollopt output, 
  9886.                              @ccopt output, 
  9887.                              @rows output, 
  9888.                              @p1,
  9889.                              @p2,
  9890.                              @p3
  9891.     end
  9892.     else if @procname = 'sp_table_privileges'
  9893.     begin
  9894.         exec @ret = sp_ddopen;11 @handle output, 
  9895.                              @scrollopt output, 
  9896.                              @ccopt output, 
  9897.                              @rows output, 
  9898.                              @p1,
  9899.                              @p2,
  9900.                              @p3
  9901.     end
  9902.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  9903.     begin
  9904.         exec @ret = sp_ddopen;12 @handle output,
  9905.                             @procname,
  9906.                             @scrollopt output, 
  9907.                             @ccopt output, 
  9908.                             @rows output, 
  9909.                             @p1,
  9910.                             @p2,
  9911.                             @p3,
  9912.                             @p4,
  9913.                             @p5
  9914.     end
  9915.     else
  9916.         print 'Unknown sp_ddopen procedure'
  9917.     select @ret = isnull(@ret,0)
  9918.     return isnull(@ret,0)
  9919. go
  9920.  
  9921.  
  9922. if (charindex('7.00', @@version) = 0 and
  9923.     charindex('8.00', @@version) = 0)
  9924. begin
  9925.     print ''
  9926.     print ''
  9927.     print 'Warning:'
  9928.     print 'you are installing the stored procedures '
  9929.     print 'on a pre 7.0 SQL Server.'
  9930.     print 'Ignore the following errors.'
  9931. end
  9932. go
  9933.  
  9934. create procedure sp_ddopen; 2(
  9935.                @handle            int output,
  9936.                @scrollopt        int output,
  9937.                @ccopt            int output,
  9938.                @rows            int output,
  9939.                @p1                nvarchar(774),
  9940.                @p2                nvarchar(774),
  9941.                @p3                nvarchar(774),
  9942.                @p4                nvarchar(774))
  9943. as
  9944.     set nocount on
  9945.     declare @ret int
  9946.  
  9947.     create table #spcolpriv (
  9948.         TABLE_QUALIFIER sysname null,
  9949.         TABLE_OWNER sysname null,
  9950.         TABLE_NAME sysname not null,
  9951.         COLUMN_NAME sysname not null,
  9952.         GRANTOR sysname null,
  9953.         GRANTEE sysname not null,
  9954.         PRIVILEGE varchar(32) not null,
  9955.         IS_GRANTABLE varchar(3) null
  9956.         )
  9957.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  9958.     exec @ret = sp_cursoropen @handle output,
  9959.         'select * from #spcolpriv',
  9960.         @scrollopt output, @ccopt output, @rows output
  9961.     drop table #spcolpriv
  9962.     return @ret
  9963. go
  9964.  
  9965.  
  9966. if (charindex('7.00', @@version) = 0 and
  9967.     charindex('8.00', @@version) = 0)
  9968. begin
  9969.     print ''
  9970.     print ''
  9971.     print 'Warning:'
  9972.     print 'you are installing the stored procedures '
  9973.     print 'on a pre 7.0 SQL Server.'
  9974.     print 'Ignore the following errors.'
  9975. end
  9976. go
  9977.  
  9978. create procedure sp_ddopen; 3(
  9979.                @handle            int output,
  9980.                @procname        sysname,
  9981.                @scrollopt        int output,
  9982.                @ccopt            int output,
  9983.                @rows            int output,
  9984.                @p1                nvarchar(774),
  9985.                @p2                nvarchar(774),
  9986.                @p3                nvarchar(774),
  9987.                @p4                nvarchar(774),
  9988.                @p5                nvarchar(774),
  9989.                @ODBCVer         int)
  9990. as
  9991.     set nocount on
  9992.     declare @ret int
  9993.  
  9994.     create table #spcolumns (
  9995.         TABLE_QUALIFIER sysname null,
  9996.         TABLE_OWNER sysname null,
  9997.         TABLE_NAME sysname not null,
  9998.         COLUMN_NAME sysname not null,
  9999.         DATA_TYPE smallint not null,
  10000.         TYPE_NAME sysname not null,
  10001.         "PRECISION" int null,
  10002.         LENGTH int null,
  10003.         SCALE smallint null,
  10004.         RADIX smallint null,
  10005.         NULLABLE smallint not null,
  10006.         REMARKS varchar(254) null,
  10007.         COLUMN_DEF nvarchar(3000) null,
  10008.         SQL_DATA_TYPE smallint not null,
  10009.         SQL_DATETIME_SUB smallint null,
  10010.         CHAR_OCTET_LENGTH int null,
  10011.         ORDINAL_POSITION int not null,
  10012.         IS_NULLABLE varchar(254) null,
  10013.         SS_DATA_TYPE tinyint null
  10014.         )
  10015.  
  10016.     if @procname = 'sp_columns'
  10017.     begin
  10018.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10019.     end
  10020.     else
  10021.     begin
  10022.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10023.     end
  10024.     exec @ret = sp_cursoropen @handle output,
  10025.         'select * from #spcolumns',
  10026.         @scrollopt output, @ccopt output, @rows output
  10027.     drop table #spcolumns
  10028.     return @ret
  10029. go
  10030.  
  10031.  
  10032.  
  10033. if (charindex('7.00', @@version) = 0 and
  10034.     charindex('8.00', @@version) = 0)
  10035. begin
  10036.     print ''
  10037.     print ''
  10038.     print 'Warning:'
  10039.     print 'you are installing the stored procedures '
  10040.     print 'on a pre 7.0 SQL Server.'
  10041.     print 'Ignore the following errors.'
  10042. end
  10043. go
  10044.  
  10045. create procedure sp_ddopen; 4(
  10046.                @handle            int output,
  10047.                @scrollopt        int output,
  10048.                @ccopt            int output,
  10049.                @rows            int output,
  10050.                @p7                int,
  10051.                @ODBCVer         int)
  10052. as
  10053.     set nocount on
  10054.     declare @ret int
  10055.  
  10056.     create table #spdatatypeinfo (
  10057.         TYPE_NAME            sysname  not null,
  10058.         DATA_TYPE            smallint not null,
  10059.         "PRECISION"            int null,
  10060.         LITERAL_PREFIX        varchar(32)    null,
  10061.         LITERAL_SUFFIX        varchar(32)    null,
  10062.         CREATE_PARAMS        varchar(32)    null,
  10063.         NULLABLE            smallint   not null,
  10064.         CASE_SENSITIVE        smallint   not null,
  10065.         SEARCHABLE            smallint   not null,
  10066.         UNSIGNED_ATTRIBUTE    smallint   null,
  10067.         MONEY    smallint    not null,
  10068.         AUTO_INCREMENT        smallint    null,
  10069.         LOCAL_TYPE_NAME     sysname null,
  10070.         MINIMUM_SCALE        smallint     null,
  10071.         MAXIMUM_SCALE        smallint   null,
  10072.         SQL_DATA_TYPE        smallint      not null,
  10073.         SQL_DATETIME_SUB    smallint   null,
  10074.         NUM_PREC_RADIX        int     null,
  10075.         INTERVAL_PRECISION    smallint    NULL,
  10076.         USERTYPE            smallint not null)
  10077.  
  10078.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10079.     exec @ret = sp_cursoropen @handle output,
  10080.         'select * from #spdatatypeinfo',
  10081.         @scrollopt output, @ccopt output, @rows output
  10082.     drop table #spdatatypeinfo
  10083.     return @ret
  10084. go
  10085.  
  10086.  
  10087.  
  10088. if (charindex('7.00', @@version) = 0 and
  10089.     charindex('8.00', @@version) = 0)
  10090. begin
  10091.     print ''
  10092.     print ''
  10093.     print 'Warning:'
  10094.     print 'you are installing the stored procedures '
  10095.     print 'on a pre 7.0 SQL Server.'
  10096.     print 'Ignore the following errors.'
  10097. end
  10098. go
  10099.  
  10100. create procedure sp_ddopen; 5(
  10101.                @handle            int output,
  10102.                @scrollopt        int output,
  10103.                @ccopt            int output,
  10104.                @rows            int output,
  10105.                @p1                nvarchar(774),
  10106.                @p2                nvarchar(774),
  10107.                @p3                nvarchar(774),
  10108.                @p4                nvarchar(774),
  10109.                @p5                nvarchar(774),
  10110.                @p6                nvarchar(774))
  10111. as
  10112.     set nocount on
  10113.     declare @ret int
  10114.  
  10115.     create table #spfkeys (
  10116.         PKTABLE_QUALIFIER sysname     null,
  10117.         PKTABLE_OWNER sysname    null,
  10118.         PKTABLE_NAME sysname  not null,
  10119.         PKCOLUMN_NAME sysname  not null,
  10120.         FKTABLE_QUALIFIER sysname    null,
  10121.         FKTABLE_OWNER sysname    null,
  10122.         FKTABLE_NAME sysname  not null,
  10123.         FKCOLUMN_NAME sysname  not null,
  10124.         KEY_SEQ smallint not null,
  10125.         UPDATE_RULE smallint null,
  10126.         DELETE_RULE smallint null,
  10127.         FK_NAME sysname null,
  10128.         PK_NAME sysname null,
  10129.         DEFERRABILITY smallint null
  10130.         )
  10131.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10132.     exec @ret = sp_cursoropen @handle output,
  10133.         'select * from #spfkeys',
  10134.         @scrollopt output, @ccopt output, @rows output
  10135.     drop table #spfkeys
  10136.     return @ret
  10137. go
  10138.  
  10139.  
  10140.  
  10141. if (charindex('7.00', @@version) = 0 and
  10142.     charindex('8.00', @@version) = 0)
  10143. begin
  10144.     print ''
  10145.     print ''
  10146.     print 'Warning:'
  10147.     print 'you are installing the stored procedures '
  10148.     print 'on a pre 7.0 SQL Server.'
  10149.     print 'Ignore the following errors.'
  10150. end
  10151. go
  10152.  
  10153. create procedure sp_ddopen; 6(
  10154.                @handle            int output,
  10155.                @scrollopt        int output,
  10156.                @ccopt            int output,
  10157.                @rows            int output,
  10158.                @p1                nvarchar(774),
  10159.                @p2                nvarchar(774),
  10160.                @p3                nvarchar(774))
  10161. as
  10162.     set nocount on
  10163.     declare @ret int
  10164.  
  10165.     create table #sppkeys (
  10166.         TABLE_QUALIFIER sysname   null,
  10167.         TABLE_OWNER sysname   null,
  10168.         TABLE_NAME sysname    not null,
  10169.         COLUMN_NAME sysname  not null,
  10170.         KEY_SEQ smallint not null,
  10171.         PK_NAME sysname null
  10172.         )
  10173.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  10174.     exec @ret = sp_cursoropen @handle output,
  10175.         'select * from #sppkeys',
  10176.         @scrollopt output, @ccopt output, @rows output
  10177.     drop table #sppkeys
  10178.     return @ret
  10179. go
  10180.  
  10181.  
  10182.  
  10183. if (charindex('7.00', @@version) = 0 and
  10184.     charindex('8.00', @@version) = 0)
  10185. begin
  10186.     print ''
  10187.     print ''
  10188.     print 'Warning:'
  10189.     print 'you are installing the stored procedures '
  10190.     print 'on a pre 7.0 SQL Server.'
  10191.     print 'Ignore the following errors.'
  10192. end
  10193. go
  10194.  
  10195. create procedure sp_ddopen; 7(
  10196.                @handle            int output,
  10197.                @scrollopt        int output,
  10198.                @ccopt            int output,
  10199.                @rows            int output,
  10200.                @p1                nvarchar(774),
  10201.                @p2                nvarchar(774),
  10202.                @p3                nvarchar(774),
  10203.                @p4                nvarchar(774),
  10204.                @p5                nvarchar(774),
  10205.                @p6                nvarchar(774),
  10206.                @ODBCVer         int)
  10207. as
  10208.     set nocount on
  10209.     declare @ret int
  10210.  
  10211.     create table #spspeccol (
  10212.         SCOPE smallint null,
  10213.         COLUMN_NAME sysname not null,
  10214.         DATA_TYPE smallint not null,
  10215.         TYPE_NAME sysname not null,
  10216.         "PRECISION" int null,
  10217.         LENGTH int null,
  10218.         SCALE smallint null,
  10219.         PSEUDO_COLUMN smallint null
  10220.         )
  10221.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  10222.     exec @ret = sp_cursoropen @handle output,
  10223.         'select * from #spspeccol',
  10224.         @scrollopt output, @ccopt output, @rows output
  10225.     drop table #spspeccol
  10226.     return @ret
  10227. go
  10228.  
  10229.  
  10230.  
  10231. if (charindex('7.00', @@version) = 0 and
  10232.     charindex('8.00', @@version) = 0)
  10233. begin
  10234.     print ''
  10235.     print ''
  10236.     print 'Warning:'
  10237.     print 'you are installing the stored procedures '
  10238.     print 'on a pre 7.0 SQL Server.'
  10239.     print 'Ignore the following errors.'
  10240. end
  10241. go
  10242.  
  10243. create procedure sp_ddopen; 8(
  10244.                @handle            int output,
  10245.                @scrollopt        int output,
  10246.                @ccopt            int output,
  10247.                @rows            int output,
  10248.                @p1                nvarchar(774),
  10249.                @p2                nvarchar(774),
  10250.                @p3                nvarchar(774),
  10251.                @p4                nvarchar(774),
  10252.                @ODBCVer         int)
  10253. as
  10254.     set nocount on
  10255.     declare @ret int
  10256.  
  10257.     create table #spproccol (
  10258.         PROCEDURE_QUALIFIER sysname  null,
  10259.         PROCEDURE_OWNER sysname  null,
  10260.         PROCEDURE_NAME sysname not null,
  10261.         COLUMN_NAME sysname not null,
  10262.         COLUMN_TYPE smallint not null,
  10263.         DATA_TYPE smallint not null,
  10264.         TYPE_NAME sysname not null,
  10265.         "PRECISION" int null,
  10266.         LENGTH int null,
  10267.         SCALE smallint null,
  10268.         RADIX smallint null,
  10269.         NULLABLE smallint not null,
  10270.         REMARKS varchar(254) null,
  10271.         COLUMN_DEF nvarchar(3000) null,
  10272.         SQL_DATA_TYPE smallint not null,
  10273.         SQL_DATETIME_SUB smallint null,
  10274.         CHAR_OCTET_LENGTH int null,
  10275.         ORDINAL_POSITION int not null,
  10276.         IS_NULLABLE varchar(254) null,
  10277.         SS_DATA_TYPE tinyint null
  10278.         )
  10279.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  10280.     exec @ret = sp_cursoropen @handle output,
  10281.         'select * from #spproccol',
  10282.         @scrollopt output, @ccopt output, @rows output
  10283.     drop table #spproccol
  10284.     return @ret
  10285. go
  10286.  
  10287.  
  10288.  
  10289. if (charindex('7.00', @@version) = 0 and
  10290.     charindex('8.00', @@version) = 0)
  10291. begin
  10292.     print ''
  10293.     print ''
  10294.     print 'Warning:'
  10295.     print 'you are installing the stored procedures '
  10296.     print 'on a pre 7.0 SQL Server.'
  10297.     print 'Ignore the following errors.'
  10298. end
  10299. go
  10300.  
  10301. create procedure sp_ddopen; 9(
  10302.                @handle            int output,
  10303.                @scrollopt        int output,
  10304.                @ccopt            int output,
  10305.                @rows            int output,
  10306.                @p1                nvarchar(774),
  10307.                @p2                nvarchar(774),
  10308.                @p3                nvarchar(774),
  10309.                @p4                nvarchar(774),
  10310.                @p5                nvarchar(774),
  10311.                @p6                nvarchar(774))
  10312. as
  10313.     set nocount on
  10314.     declare @ret int
  10315.  
  10316.     create table #spstatistics (
  10317.         TABLE_QUALIFIER sysname   null,
  10318.         TABLE_OWNER sysname   null,
  10319.         TABLE_NAME sysname    not null,
  10320.         NON_UNIQUE smallint null,
  10321.         INDEX_QUALIFIER sysname null,
  10322.         INDEX_NAME sysname null,
  10323.         TYPE smallint not null,
  10324.         SEQ_IN_INDEX smallint null,
  10325.         COLUMN_NAME sysname null,
  10326.         COLLATION char(1) null,
  10327.         CARDINALITY int null,
  10328.         PAGES int null,
  10329.         FILTER_CONDITION varchar(128) null
  10330.         )
  10331.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  10332.     exec @ret = sp_cursoropen @handle output,
  10333.         'select * from #spstatistics',
  10334.         @scrollopt output, @ccopt output, @rows output
  10335.     drop table #spstatistics
  10336.     return @ret
  10337. go
  10338.                
  10339.  
  10340.  
  10341. if (charindex('7.00', @@version) = 0 and
  10342.     charindex('8.00', @@version) = 0)
  10343. begin
  10344.     print ''
  10345.     print ''
  10346.     print 'Warning:'
  10347.     print 'you are installing the stored procedures '
  10348.     print 'on a pre 7.0 SQL Server.'
  10349.     print 'Ignore the following errors.'
  10350. end
  10351. go
  10352.  
  10353. create procedure sp_ddopen; 10(
  10354.                @handle            int output,
  10355.                @scrollopt        int output,
  10356.                @ccopt            int output,
  10357.                @rows            int output,
  10358.                @p1                nvarchar(774),
  10359.                @p2                nvarchar(774),
  10360.                @p3                nvarchar(774))
  10361. as
  10362.     set nocount on
  10363.     declare @ret int
  10364.  
  10365.     create table #spprocedures (
  10366.         PROCEDURE_QUALIFIER sysname  null,
  10367.         PROCEDURE_OWNER sysname  null,
  10368.         PROCEDURE_NAME sysname not null,
  10369.         NUM_INPUT_PARAMS int null,
  10370.         NUM_OUTPUT_PARAMS int null,
  10371.         NUM_RESULT_SETS int null,
  10372.         REMARKS varchar(254) null,
  10373.         PROCEDURE_TYPE smallint null
  10374.         )
  10375.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  10376.     exec @ret = sp_cursoropen @handle output,
  10377.         'select * from #spprocedures',
  10378.         @scrollopt output, @ccopt output, @rows output
  10379.     drop table #spprocedures
  10380.     return @ret
  10381. go
  10382.  
  10383.  
  10384.  
  10385. if (charindex('7.00', @@version) = 0 and
  10386.     charindex('8.00', @@version) = 0)
  10387. begin
  10388.     print ''
  10389.     print ''
  10390.     print 'Warning:'
  10391.     print 'you are installing the stored procedures '
  10392.     print 'on a pre 7.0 SQL Server.'
  10393.     print 'Ignore the following errors.'
  10394. end
  10395. go
  10396.  
  10397. create procedure sp_ddopen; 11(
  10398.                @handle            int output,
  10399.                @scrollopt        int output,
  10400.                @ccopt            int output,
  10401.                @rows            int output,
  10402.                @p1                nvarchar(774),
  10403.                @p2                nvarchar(774),
  10404.                @p3                nvarchar(774))
  10405. as
  10406.     set nocount on
  10407.     declare @ret int
  10408.  
  10409.     create table #sptabpriv (
  10410.         TABLE_QUALIFIER sysname null,
  10411.         TABLE_OWNER sysname null,
  10412.         TABLE_NAME sysname not null,
  10413.         GRANTOR sysname null,
  10414.         GRANTEE sysname not null,
  10415.         PRIVILEGE varchar(32) not null,
  10416.         IS_GRANTABLE varchar(3) null
  10417.         )
  10418.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  10419.     exec @ret = sp_cursoropen @handle output,
  10420.         'select * from #sptabpriv',
  10421.         @scrollopt output, @ccopt output, @rows output
  10422.     drop table #sptabpriv
  10423.     return @ret
  10424. go
  10425.  
  10426.  
  10427.  
  10428. if (charindex('7.00', @@version) = 0 and
  10429.     charindex('8.00', @@version) = 0)
  10430. begin
  10431.     print ''
  10432.     print ''
  10433.     print 'Warning:'
  10434.     print 'you are installing the stored procedures '
  10435.     print 'on a pre 7.0 SQL Server.'
  10436.     print 'Ignore the following errors.'
  10437. end
  10438. go
  10439.  
  10440. create procedure sp_ddopen; 12(
  10441.                @handle            int output,
  10442.                @procname        sysname,
  10443.                @scrollopt        int output,
  10444.                @ccopt            int output,
  10445.                @rows            int output,
  10446.                @p1                nvarchar(774),
  10447.                @p2                nvarchar(774),
  10448.                @p3                nvarchar(774),
  10449.                @p4                nvarchar(774),
  10450.                @p5                nvarchar(774))
  10451. as
  10452.     set nocount on
  10453.     declare @ret int
  10454.  
  10455.     create table #sptables (
  10456.         TABLE_QUALIFIER sysname null,
  10457.         TABLE_OWNER sysname null,
  10458.         TABLE_NAME sysname null,
  10459.         TABLE_TYPE    varchar(32) null,
  10460.         REMARKS varchar(254) null)
  10461.     if @procname = 'sp_tables'
  10462.     begin
  10463.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  10464.     end
  10465.     else
  10466.     begin
  10467.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  10468.     end
  10469.     exec @ret = sp_cursoropen @handle output,
  10470.         'select * from #sptables',
  10471.         @scrollopt output, @ccopt output, @rows output
  10472.     drop table #sptables
  10473.     return @ret
  10474. go
  10475.  
  10476. if (charindex('8.00', @@version) > 0)
  10477.     drop procedure sp_ddopen
  10478. else
  10479. begin
  10480.     print ''
  10481.     print ''
  10482.     print 'Warning:'
  10483.     print 'you are installing the stored procedures '
  10484.     print 'on a pre 8.0 SQL Server.'
  10485.     print 'Ignore the following errors.'
  10486. end
  10487. go
  10488.  
  10489. /*    Procedure for 8.0 server */
  10490. create procedure sp_ddopen; 1(
  10491.                @handle            int output,
  10492.                @procname        sysname,
  10493.                @scrollopt        int output,
  10494.                @ccopt            int output,
  10495.                @rows            int output,
  10496.                @p1                nvarchar(774) = null,
  10497.                @p2                nvarchar(774) = null,
  10498.                @p3                nvarchar(774) = null,
  10499.                @p4                nvarchar(774) = null,
  10500.                @p5                nvarchar(774) = null,
  10501.                @p6                nvarchar(774) = null,
  10502.                @p7                int = null,
  10503.                @ODBCVer         int = 2)
  10504. as
  10505.     set nocount on
  10506.     declare @ret int
  10507.  
  10508.     if @procname = 'sp_column_privileges'
  10509.     begin
  10510.         exec @ret = sp_ddopen;2 @handle output, 
  10511.                             @scrollopt output, 
  10512.                             @ccopt output, 
  10513.                             @rows output, 
  10514.                             @p1,
  10515.                             @p2,
  10516.                             @p3,
  10517.                             @p4
  10518.     end
  10519.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  10520.     begin
  10521.         exec @ret = sp_ddopen;3 @handle output,
  10522.                             @procname,
  10523.                             @scrollopt output, 
  10524.                             @ccopt output, 
  10525.                             @rows output, 
  10526.                             @p1,
  10527.                             @p2,
  10528.                             @p3,
  10529.                             @p4,
  10530.                             @p5,
  10531.                             @ODBCVer
  10532.     end
  10533.     else if @procname = 'sp_datatype_info'
  10534.     begin
  10535.         exec @ret = sp_ddopen;4 @handle output,
  10536.                             @scrollopt output, 
  10537.                             @ccopt output, 
  10538.                             @rows output, 
  10539.                             @p7,
  10540.                             @ODBCVer
  10541.     end
  10542.     else if @procname = 'sp_fkeys'
  10543.     begin
  10544.         exec @ret = sp_ddopen;5 @handle output, 
  10545.                             @scrollopt output, 
  10546.                             @ccopt output, 
  10547.                             @rows output, 
  10548.                             @p1,
  10549.                             @p2,
  10550.                             @p3,
  10551.                             @p4,
  10552.                             @p5,
  10553.                             @p6
  10554.     end
  10555.     else if @procname = 'sp_pkeys'
  10556.     begin
  10557.         exec @ret = sp_ddopen;6 @handle output, 
  10558.                             @scrollopt output, 
  10559.                             @ccopt output, 
  10560.                             @rows output, 
  10561.                             @p1,
  10562.                             @p2,
  10563.                             @p3
  10564.     end
  10565.     else if @procname = 'sp_special_columns'
  10566.     begin
  10567.         exec @ret = sp_ddopen;7 @handle output, 
  10568.                             @scrollopt output, 
  10569.                             @ccopt output, 
  10570.                             @rows output, 
  10571.                             @p1,
  10572.                             @p2,
  10573.                             @p3,
  10574.                             @p4,
  10575.                             @p5,
  10576.                             @p6,
  10577.                             @ODBCVer
  10578.     end
  10579.     else if @procname = 'sp_sproc_columns'
  10580.     begin
  10581.         exec @ret = sp_ddopen;8 @handle output, 
  10582.                             @scrollopt output, 
  10583.                             @ccopt output, 
  10584.                             @rows output, 
  10585.                             @p1,
  10586.                             @p2,
  10587.                             @p3,
  10588.                             @p4,
  10589.                             @ODBCVer
  10590.     end
  10591.     else if @procname = 'sp_statistics'
  10592.     begin
  10593.         exec @ret = sp_ddopen;9 @handle output, 
  10594.                             @scrollopt output, 
  10595.                             @ccopt output, 
  10596.                             @rows output, 
  10597.                             @p1,
  10598.                             @p2,
  10599.                             @p3,
  10600.                             @p4,
  10601.                             @p5,
  10602.                             @p6
  10603.     end
  10604.     else if @procname = 'sp_stored_procedures'
  10605.     begin
  10606.         exec @ret = sp_ddopen;10 @handle output, 
  10607.                              @scrollopt output, 
  10608.                              @ccopt output, 
  10609.                              @rows output, 
  10610.                              @p1,
  10611.                              @p2,
  10612.                              @p3
  10613.     end
  10614.     else if @procname = 'sp_table_privileges'
  10615.     begin
  10616.         exec @ret = sp_ddopen;11 @handle output, 
  10617.                              @scrollopt output, 
  10618.                              @ccopt output, 
  10619.                              @rows output, 
  10620.                              @p1,
  10621.                              @p2,
  10622.                              @p3
  10623.     end
  10624.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  10625.     begin
  10626.         exec @ret = sp_ddopen;12 @handle output,
  10627.                             @procname,
  10628.                             @scrollopt output, 
  10629.                             @ccopt output, 
  10630.                             @rows output, 
  10631.                             @p1,
  10632.                             @p2,
  10633.                             @p3,
  10634.                             @p4,
  10635.                             @p5
  10636.     end
  10637.     else
  10638.         print 'Unknown sp_ddopen procedure'
  10639.     select @ret = isnull(@ret,0)
  10640.     return isnull(@ret,0)
  10641. go
  10642.  
  10643.  
  10644. if (charindex('8.00', @@version) = 0)
  10645. begin
  10646.     print ''
  10647.     print ''
  10648.     print 'Warning:'
  10649.     print 'you are installing the stored procedures '
  10650.     print 'on a pre 8.0 SQL Server.'
  10651.     print 'Ignore the following errors.'
  10652. end
  10653. go
  10654.  
  10655. create procedure sp_ddopen; 2(
  10656.                @handle            int output,
  10657.                @scrollopt        int output,
  10658.                @ccopt            int output,
  10659.                @rows            int output,
  10660.                @p1                nvarchar(774),
  10661.                @p2                nvarchar(774),
  10662.                @p3                nvarchar(774),
  10663.                @p4                nvarchar(774))
  10664. as
  10665.     set nocount on
  10666.     declare @ret int
  10667.  
  10668.     create table #spcolpriv (
  10669.         TABLE_QUALIFIER sysname collate database_default null,
  10670.         TABLE_OWNER sysname collate database_default null,
  10671.         TABLE_NAME sysname collate database_default not null,
  10672.         COLUMN_NAME sysname collate database_default not null,
  10673.         GRANTOR sysname collate database_default null,
  10674.         GRANTEE sysname collate database_default not null,
  10675.         PRIVILEGE varchar(32) collate database_default not null,
  10676.         IS_GRANTABLE varchar(3) collate database_default null
  10677.         )
  10678.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  10679.     exec @ret = sp_cursoropen @handle output,
  10680.         'select * from #spcolpriv',
  10681.         @scrollopt output, @ccopt output, @rows output
  10682.     drop table #spcolpriv
  10683.     return @ret
  10684. go
  10685.  
  10686.  
  10687. if (charindex('8.00', @@version) = 0)
  10688. begin
  10689.     print ''
  10690.     print ''
  10691.     print 'Warning:'
  10692.     print 'you are installing the stored procedures '
  10693.     print 'on a pre 8.0 SQL Server.'
  10694.     print 'Ignore the following errors.'
  10695. end
  10696. go
  10697.  
  10698. create procedure sp_ddopen; 3(
  10699.                @handle            int output,
  10700.                @procname        sysname,
  10701.                @scrollopt        int output,
  10702.                @ccopt            int output,
  10703.                @rows            int output,
  10704.                @p1                nvarchar(774),
  10705.                @p2                nvarchar(774),
  10706.                @p3                nvarchar(774),
  10707.                @p4                nvarchar(774),
  10708.                @p5                nvarchar(774),
  10709.                @ODBCVer         int)
  10710. as
  10711.     set nocount on
  10712.     declare @ret int
  10713.  
  10714.     create table #spcolumns (
  10715.         TABLE_QUALIFIER sysname collate database_default null,
  10716.         TABLE_OWNER sysname collate database_default null,
  10717.         TABLE_NAME sysname collate database_default not null,
  10718.         COLUMN_NAME sysname collate database_default not null,
  10719.         DATA_TYPE smallint not null,
  10720.         TYPE_NAME sysname collate database_default not null,
  10721.         "PRECISION" int null,
  10722.         LENGTH int null,
  10723.         SCALE smallint null,
  10724.         RADIX smallint null,
  10725.         NULLABLE smallint not null,
  10726.         REMARKS varchar(254) collate database_default null,
  10727.         COLUMN_DEF nvarchar(3000) collate database_default null,
  10728.         SQL_DATA_TYPE smallint null,
  10729.         SQL_DATETIME_SUB smallint null,
  10730.         CHAR_OCTET_LENGTH int null,
  10731.         ORDINAL_POSITION int not null,
  10732.         IS_NULLABLE varchar(254) collate database_default null,
  10733.         SS_DATA_TYPE tinyint null
  10734.         )
  10735.  
  10736.     if @procname = 'sp_columns'
  10737.     begin
  10738.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10739.     end
  10740.     else
  10741.     begin
  10742.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10743.     end
  10744.     exec @ret = sp_cursoropen @handle output,
  10745.         'select * from #spcolumns',
  10746.         @scrollopt output, @ccopt output, @rows output
  10747.     drop table #spcolumns
  10748.     return @ret
  10749. go
  10750.  
  10751.  
  10752.  
  10753. if (charindex('8.00', @@version) = 0)
  10754. begin
  10755.     print ''
  10756.     print ''
  10757.     print 'Warning:'
  10758.     print 'you are installing the stored procedures '
  10759.     print 'on a pre 8.0 SQL Server.'
  10760.     print 'Ignore the following errors.'
  10761. end
  10762. go
  10763.  
  10764. create procedure sp_ddopen; 4(
  10765.                @handle            int output,
  10766.                @scrollopt        int output,
  10767.                @ccopt            int output,
  10768.                @rows            int output,
  10769.                @p7                int,
  10770.                @ODBCVer         int)
  10771. as
  10772.     set nocount on
  10773.     declare @ret int
  10774.  
  10775.     create table #spdatatypeinfo (
  10776.         TYPE_NAME            sysname  collate database_default not null,
  10777.         DATA_TYPE            smallint not null,
  10778.         "PRECISION"            int null,
  10779.         LITERAL_PREFIX        varchar(32)    collate database_default null,
  10780.         LITERAL_SUFFIX        varchar(32)    collate database_default null,
  10781.         CREATE_PARAMS        varchar(32)    collate database_default null,
  10782.         NULLABLE            smallint   not null,
  10783.         CASE_SENSITIVE        smallint   not null,
  10784.         SEARCHABLE            smallint   not null,
  10785.         UNSIGNED_ATTRIBUTE    smallint   null,
  10786.         MONEY    smallint    not null,
  10787.         AUTO_INCREMENT        smallint    null,
  10788.         LOCAL_TYPE_NAME     sysname collate database_default null,
  10789.         MINIMUM_SCALE        smallint     null,
  10790.         MAXIMUM_SCALE        smallint   null,
  10791.         SQL_DATA_TYPE        smallint      not null,
  10792.         SQL_DATETIME_SUB    smallint   null,
  10793.         NUM_PREC_RADIX        int     null,
  10794.         INTERVAL_PRECISION    smallint    NULL,
  10795.         USERTYPE            smallint not null)
  10796.  
  10797.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10798.     exec @ret = sp_cursoropen @handle output,
  10799.         'select * from #spdatatypeinfo',
  10800.         @scrollopt output, @ccopt output, @rows output
  10801.     drop table #spdatatypeinfo
  10802.     return @ret
  10803. go
  10804.  
  10805.  
  10806.  
  10807. if (charindex('8.00', @@version) = 0)
  10808. begin
  10809.     print ''
  10810.     print ''
  10811.     print 'Warning:'
  10812.     print 'you are installing the stored procedures '
  10813.     print 'on a pre 8.0 SQL Server.'
  10814.     print 'Ignore the following errors.'
  10815. end
  10816. go
  10817.  
  10818. create procedure sp_ddopen; 5(
  10819.                @handle            int output,
  10820.                @scrollopt        int output,
  10821.                @ccopt            int output,
  10822.                @rows            int output,
  10823.                @p1                nvarchar(774),
  10824.                @p2                nvarchar(774),
  10825.                @p3                nvarchar(774),
  10826.                @p4                nvarchar(774),
  10827.                @p5                nvarchar(774),
  10828.                @p6                nvarchar(774))
  10829. as
  10830.     set nocount on
  10831.     declare @ret int
  10832.  
  10833.     create table #spfkeys (
  10834.         PKTABLE_QUALIFIER sysname     collate database_default null,
  10835.         PKTABLE_OWNER sysname    collate database_default null,
  10836.         PKTABLE_NAME sysname  collate database_default not null,
  10837.         PKCOLUMN_NAME sysname  collate database_default not null,
  10838.         FKTABLE_QUALIFIER sysname    collate database_default null,
  10839.         FKTABLE_OWNER sysname    collate database_default null,
  10840.         FKTABLE_NAME sysname  collate database_default not null,
  10841.         FKCOLUMN_NAME sysname  collate database_default not null,
  10842.         KEY_SEQ smallint not null,
  10843.         UPDATE_RULE smallint null,
  10844.         DELETE_RULE smallint null,
  10845.         FK_NAME sysname collate database_default null,
  10846.         PK_NAME sysname collate database_default null,
  10847.         DEFERRABILITY smallint null
  10848.         )
  10849.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10850.     exec @ret = sp_cursoropen @handle output,
  10851.         'select * from #spfkeys',
  10852.         @scrollopt output, @ccopt output, @rows output
  10853.     drop table #spfkeys
  10854.     return @ret
  10855. go
  10856.  
  10857.  
  10858.  
  10859. if (charindex('8.00', @@version) = 0)
  10860. begin
  10861.     print ''
  10862.     print ''
  10863.     print 'Warning:'
  10864.     print 'you are installing the stored procedures '
  10865.     print 'on a pre 8.0 SQL Server.'
  10866.     print 'Ignore the following errors.'
  10867. end
  10868. go
  10869.  
  10870. create procedure sp_ddopen; 6(
  10871.                @handle            int output,
  10872.                @scrollopt        int output,
  10873.                @ccopt            int output,
  10874.                @rows            int output,
  10875.                @p1                nvarchar(774),
  10876.                @p2                nvarchar(774),
  10877.                @p3                nvarchar(774))
  10878. as
  10879.     set nocount on
  10880.     declare @ret int
  10881.  
  10882.     create table #sppkeys (
  10883.         TABLE_QUALIFIER sysname   collate database_default null,
  10884.         TABLE_OWNER sysname   collate database_default null,
  10885.         TABLE_NAME sysname    collate database_default not null,
  10886.         COLUMN_NAME sysname  collate database_default not null,
  10887.         KEY_SEQ smallint not null,
  10888.         PK_NAME sysname collate database_default null
  10889.         )
  10890.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  10891.     exec @ret = sp_cursoropen @handle output,
  10892.         'select * from #sppkeys',
  10893.         @scrollopt output, @ccopt output, @rows output
  10894.     drop table #sppkeys
  10895.     return @ret
  10896. go
  10897.  
  10898.  
  10899.  
  10900. if (charindex('8.00', @@version) = 0)
  10901. begin
  10902.     print ''
  10903.     print ''
  10904.     print 'Warning:'
  10905.     print 'you are installing the stored procedures '
  10906.     print 'on a pre 8.0 SQL Server.'
  10907.     print 'Ignore the following errors.'
  10908. end
  10909. go
  10910.  
  10911. create procedure sp_ddopen; 7(
  10912.                @handle            int output,
  10913.                @scrollopt        int output,
  10914.                @ccopt            int output,
  10915.                @rows            int output,
  10916.                @p1                nvarchar(774),
  10917.                @p2                nvarchar(774),
  10918.                @p3                nvarchar(774),
  10919.                @p4                nvarchar(774),
  10920.                @p5                nvarchar(774),
  10921.                @p6                nvarchar(774),
  10922.                @ODBCVer         int)
  10923. as
  10924.     set nocount on
  10925.     declare @ret int
  10926.  
  10927.     create table #spspeccol (
  10928.         SCOPE smallint null,
  10929.         COLUMN_NAME sysname collate database_default not null,
  10930.         DATA_TYPE smallint not null,
  10931.         TYPE_NAME sysname collate database_default not null,
  10932.         "PRECISION" int null,
  10933.         LENGTH int null,
  10934.         SCALE smallint null,
  10935.         PSEUDO_COLUMN smallint null
  10936.         )
  10937.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  10938.     exec @ret = sp_cursoropen @handle output,
  10939.         'select * from #spspeccol',
  10940.         @scrollopt output, @ccopt output, @rows output
  10941.     drop table #spspeccol
  10942.     return @ret
  10943. go
  10944.  
  10945.  
  10946.  
  10947. if (charindex('8.00', @@version) = 0)
  10948. begin
  10949.     print ''
  10950.     print ''
  10951.     print 'Warning:'
  10952.     print 'you are installing the stored procedures '
  10953.     print 'on a pre 8.0 SQL Server.'
  10954.     print 'Ignore the following errors.'
  10955. end
  10956. go
  10957.  
  10958. create procedure sp_ddopen; 8(
  10959.                @handle            int output,
  10960.                @scrollopt        int output,
  10961.                @ccopt            int output,
  10962.                @rows            int output,
  10963.                @p1                nvarchar(774),
  10964.                @p2                nvarchar(774),
  10965.                @p3                nvarchar(774),
  10966.                @p4                nvarchar(774),
  10967.                @ODBCVer         int)
  10968. as
  10969.     set nocount on
  10970.     declare @ret int
  10971.  
  10972.     create table #spproccol (
  10973.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  10974.         PROCEDURE_OWNER sysname  collate database_default null,
  10975.         PROCEDURE_NAME sysname collate database_default not null,
  10976.         COLUMN_NAME sysname collate database_default not null,
  10977.         COLUMN_TYPE smallint not null,
  10978.         DATA_TYPE smallint null,
  10979.         TYPE_NAME sysname collate database_default not null,
  10980.         "PRECISION" int null,
  10981.         LENGTH int null,
  10982.         SCALE smallint null,
  10983.         RADIX smallint null,
  10984.         NULLABLE smallint not null,
  10985.         REMARKS varchar(254) collate database_default null,
  10986.         COLUMN_DEF nvarchar(3000) collate database_default null,
  10987.         SQL_DATA_TYPE smallint null,
  10988.         SQL_DATETIME_SUB smallint null,
  10989.         CHAR_OCTET_LENGTH int null,
  10990.         ORDINAL_POSITION int not null,
  10991.         IS_NULLABLE varchar(254) collate database_default null,
  10992.         SS_DATA_TYPE tinyint null
  10993.         )
  10994.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  10995.     exec @ret = sp_cursoropen @handle output,
  10996.         'select * from #spproccol',
  10997.         @scrollopt output, @ccopt output, @rows output
  10998.     drop table #spproccol
  10999.     return @ret
  11000. go
  11001.  
  11002.  
  11003.  
  11004. if (charindex('8.00', @@version) = 0)
  11005. begin
  11006.     print ''
  11007.     print ''
  11008.     print 'Warning:'
  11009.     print 'you are installing the stored procedures '
  11010.     print 'on a pre 8.0 SQL Server.'
  11011.     print 'Ignore the following errors.'
  11012. end
  11013. go
  11014.  
  11015. create procedure sp_ddopen; 9(
  11016.                @handle            int output,
  11017.                @scrollopt        int output,
  11018.                @ccopt            int output,
  11019.                @rows            int output,
  11020.                @p1                nvarchar(774),
  11021.                @p2                nvarchar(774),
  11022.                @p3                nvarchar(774),
  11023.                @p4                nvarchar(774),
  11024.                @p5                nvarchar(774),
  11025.                @p6                nvarchar(774))
  11026. as
  11027.     set nocount on
  11028.     declare @ret int
  11029.  
  11030.     create table #spstatistics (
  11031.         TABLE_QUALIFIER sysname   collate database_default null,
  11032.         TABLE_OWNER sysname   collate database_default null,
  11033.         TABLE_NAME sysname    collate database_default not null,
  11034.         NON_UNIQUE smallint null,
  11035.         INDEX_QUALIFIER sysname collate database_default null,
  11036.         INDEX_NAME sysname collate database_default null,
  11037.         TYPE smallint not null,
  11038.         SEQ_IN_INDEX smallint null,
  11039.         COLUMN_NAME sysname collate database_default null,
  11040.         COLLATION char(1) collate database_default null,
  11041.         CARDINALITY int null,
  11042.         PAGES int null,
  11043.         FILTER_CONDITION varchar(128) collate database_default null
  11044.         )
  11045.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  11046.     exec @ret = sp_cursoropen @handle output,
  11047.         'select * from #spstatistics',
  11048.         @scrollopt output, @ccopt output, @rows output
  11049.     drop table #spstatistics
  11050.     return @ret
  11051. go
  11052.                
  11053.  
  11054.  
  11055. if (charindex('8.00', @@version) = 0)
  11056. begin
  11057.     print ''
  11058.     print ''
  11059.     print 'Warning:'
  11060.     print 'you are installing the stored procedures '
  11061.     print 'on a pre 8.0 SQL Server.'
  11062.     print 'Ignore the following errors.'
  11063. end
  11064. go
  11065.  
  11066. create procedure sp_ddopen; 10(
  11067.                @handle            int output,
  11068.                @scrollopt        int output,
  11069.                @ccopt            int output,
  11070.                @rows            int output,
  11071.                @p1                nvarchar(774),
  11072.                @p2                nvarchar(774),
  11073.                @p3                nvarchar(774))
  11074. as
  11075.     set nocount on
  11076.     declare @ret int
  11077.  
  11078.     create table #spprocedures (
  11079.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  11080.         PROCEDURE_OWNER sysname  collate database_default null,
  11081.         PROCEDURE_NAME sysname collate database_default not null,
  11082.         NUM_INPUT_PARAMS int null,
  11083.         NUM_OUTPUT_PARAMS int null,
  11084.         NUM_RESULT_SETS int null,
  11085.         REMARKS varchar(254) collate database_default null,
  11086.         PROCEDURE_TYPE smallint null
  11087.         )
  11088.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  11089.     exec @ret = sp_cursoropen @handle output,
  11090.         'select * from #spprocedures',
  11091.         @scrollopt output, @ccopt output, @rows output
  11092.     drop table #spprocedures
  11093.     return @ret
  11094. go
  11095.  
  11096.  
  11097.  
  11098. if (charindex('8.00', @@version) = 0)
  11099. begin
  11100.     print ''
  11101.     print ''
  11102.     print 'Warning:'
  11103.     print 'you are installing the stored procedures '
  11104.     print 'on a pre 8.0 SQL Server.'
  11105.     print 'Ignore the following errors.'
  11106. end
  11107. go
  11108.  
  11109. create procedure sp_ddopen; 11(
  11110.                @handle            int output,
  11111.                @scrollopt        int output,
  11112.                @ccopt            int output,
  11113.                @rows            int output,
  11114.                @p1                nvarchar(774),
  11115.                @p2                nvarchar(774),
  11116.                @p3                nvarchar(774))
  11117. as
  11118.     set nocount on
  11119.     declare @ret int
  11120.  
  11121.     create table #sptabpriv (
  11122.         TABLE_QUALIFIER sysname collate database_default null,
  11123.         TABLE_OWNER sysname collate database_default null,
  11124.         TABLE_NAME sysname collate database_default not null,
  11125.         GRANTOR sysname collate database_default null,
  11126.         GRANTEE sysname collate database_default not null,
  11127.         PRIVILEGE varchar(32) collate database_default not null,
  11128.         IS_GRANTABLE varchar(3) collate database_default null
  11129.         )
  11130.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  11131.     exec @ret = sp_cursoropen @handle output,
  11132.         'select * from #sptabpriv',
  11133.         @scrollopt output, @ccopt output, @rows output
  11134.     drop table #sptabpriv
  11135.     return @ret
  11136. go
  11137.  
  11138.  
  11139.  
  11140. if (charindex('8.00', @@version) = 0)
  11141. begin
  11142.     print ''
  11143.     print ''
  11144.     print 'Warning:'
  11145.     print 'you are installing the stored procedures '
  11146.     print 'on a pre 8.0 SQL Server.'
  11147.     print 'Ignore the following errors.'
  11148. end
  11149. go
  11150.  
  11151. create procedure sp_ddopen; 12(
  11152.                @handle            int output,
  11153.                @procname        sysname,
  11154.                @scrollopt        int output,
  11155.                @ccopt            int output,
  11156.                @rows            int output,
  11157.                @p1                nvarchar(774),
  11158.                @p2                nvarchar(774),
  11159.                @p3                nvarchar(774),
  11160.                @p4                nvarchar(774),
  11161.                @p5                nvarchar(774))
  11162. as
  11163.     set nocount on
  11164.     declare @ret int
  11165.  
  11166.     create table #sptables (
  11167.         TABLE_QUALIFIER sysname collate database_default null,
  11168.         TABLE_OWNER sysname collate database_default null,
  11169.         TABLE_NAME sysname collate database_default null,
  11170.         TABLE_TYPE    varchar(32) collate database_default null,
  11171.         REMARKS varchar(254) collate database_default null)
  11172.     if @procname = 'sp_tables'
  11173.     begin
  11174.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  11175.     end
  11176.     else
  11177.     begin
  11178.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  11179.     end
  11180.     exec @ret = sp_cursoropen @handle output,
  11181.         'select * from #sptables',
  11182.         @scrollopt output, @ccopt output, @rows output
  11183.     drop table #sptables
  11184.     return @ret
  11185. go
  11186. grant execute on sp_ddopen to public
  11187. go
  11188.  
  11189. print 'creating sp_tableswc'
  11190. go
  11191.  
  11192. if (charindex('7.00', @@version) = 0 and
  11193.     charindex('8.00', @@version) = 0)
  11194. begin
  11195.     print ''
  11196.     print ''
  11197.     print 'Warning:'
  11198.     print 'you are installing the stored procedures '
  11199.     print 'on a pre 7.0 SQL Server.'
  11200.     print 'Ignore the following errors.'
  11201. end
  11202. go
  11203.  
  11204. /*    Procedure for 7.0 server */
  11205. create procedure sp_tableswc(
  11206.                @table_name        nvarchar(384)    = null,
  11207.                @table_owner     nvarchar(384)    = null,
  11208.                @table_qualifier sysname    = null,
  11209.                @table_type        varchar(100) = null)
  11210. as
  11211.     declare @databasename    sysname
  11212.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  11213.  
  11214.     create table #sptables (
  11215.         TABLE_QUALIFIER sysname null,
  11216.         TABLE_OWNER sysname null,
  11217.         TABLE_NAME sysname null,
  11218.         TABLE_TYPE    varchar(32) null,
  11219.         REMARKS varchar(254) null)
  11220.  
  11221.     declare databases CURSOR FOR
  11222.         select name from master..sysdatabases
  11223.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11224.         for read only
  11225.  
  11226.     open databases
  11227.     fetch next from databases into @databasename
  11228.     while (@@FETCH_STATUS <> -1)
  11229.     begin
  11230.         if (charindex('%', @databasename) = 0)
  11231.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  11232.             select @qualprocname = @databasename + '..sp_tables'
  11233.             insert into #sptables exec @qualprocname
  11234.                 @table_name, @table_owner, @databasename, @table_type
  11235.         end
  11236.         fetch next from databases into @databasename
  11237.     end
  11238.     deallocate databases
  11239.     select * from #sptables
  11240.         order by 4, 1, 2, 3
  11241. go
  11242.  
  11243. if (charindex('8.00', @@version) = 0)
  11244. begin
  11245.     print ''
  11246.     print ''
  11247.     print 'Warning:'
  11248.     print 'you are installing the stored procedures '
  11249.     print 'on a pre 8.0 SQL Server.'
  11250.     print 'Ignore the following errors.'
  11251. end
  11252. else
  11253. drop procedure sp_tableswc
  11254. go
  11255.  
  11256. /*    Procedure for 8.0 server */
  11257. create procedure sp_tableswc(
  11258.                @table_name        nvarchar(384)    = null,
  11259.                @table_owner     nvarchar(384)    = null,
  11260.                @table_qualifier sysname    = null,
  11261.                @table_type        varchar(100) = null)
  11262. as
  11263.     declare @databasename    sysname
  11264.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  11265.  
  11266.     create table #sptables (
  11267.         TABLE_QUALIFIER sysname collate database_default null,
  11268.         TABLE_OWNER sysname collate database_default null,
  11269.         TABLE_NAME sysname collate database_default null,
  11270.         TABLE_TYPE    varchar(32) collate database_default null,
  11271.         REMARKS varchar(254) collate database_default null)
  11272.  
  11273.     declare databases CURSOR FOR
  11274.         select name from master..sysdatabases
  11275.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11276.         for read only
  11277.  
  11278.     open databases
  11279.     fetch next from databases into @databasename
  11280.     while (@@FETCH_STATUS <> -1)
  11281.     begin
  11282.         if (charindex('%', @databasename) = 0)
  11283.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  11284.             select @qualprocname = @databasename + '..sp_tables'
  11285.             insert into #sptables exec @qualprocname
  11286.                 @table_name, @table_owner, @databasename, @table_type
  11287.         end
  11288.         fetch next from databases into @databasename
  11289.     end
  11290.     deallocate databases
  11291.     select * from #sptables
  11292.         order by 4, 1, 2, 3
  11293. go
  11294.  
  11295. grant execute on sp_tableswc to public
  11296. go
  11297.  
  11298. dump tran master with no_log
  11299. go
  11300.  
  11301. /*-------------------------------------------------------------------------*/
  11302. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  11303. /*-------------------------------------------------------------------------*/
  11304.  
  11305. print ''
  11306. print 'creating spt_provider_types'
  11307. go
  11308. if (charindex('7.00', @@version) = 0 and
  11309.     charindex('8.00', @@version) = 0)
  11310. begin /* Pre 8.0 Server */
  11311. create table spt_provider_types
  11312.     (
  11313.     ss_dtype        tinyint     not null,
  11314.     fixlen            int         null,        /* datatype len for variable, else null */
  11315.     type_name        sysname        not null,
  11316.     oledb_data_type        smallint    not null,
  11317.     best_match        bit        not null,
  11318.     is_nullable        tinyint        null,
  11319.     case_sensitive        bit        not null,
  11320.     fixed_prec_scale    bit        not null,
  11321.     is_long            bit        not null,
  11322.     auto_unique_value    tinyint        not null,
  11323.     data_precision        int         null,
  11324.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  11325.     column_size        int         null,
  11326.     literal_prefix        varchar(32)    null,
  11327.     literal_suffix        varchar(32)     null,
  11328.     searchable        int        not null,
  11329.     unsigned_attribute    tinyint        null,
  11330.     local_type_name     sysname        null
  11331.     )
  11332. print ''
  11333. print ''
  11334. print 'Warning:'
  11335. print 'you are installing the stored procedures '
  11336. print 'on a pre 8.0 SQL Server.'
  11337. print 'Ignore the following errors.'
  11338. end
  11339. go
  11340. if (charindex('7.00', @@version) > 0 or
  11341.     charindex('8.00', @@version) > 0)
  11342. begin /* 8.0 Server */
  11343. create table spt_provider_types
  11344.     (
  11345.     ss_dtype        tinyint     not null,
  11346.     fixlen            int         null,        /* datatype len for variable, else null */
  11347.     type_name        sysname        not null,
  11348.     oledb_data_type        smallint    not null,
  11349.     best_match        bit        not null,
  11350.     is_nullable        bit        null,
  11351.     case_sensitive        bit        not null,
  11352.     fixed_prec_scale    bit        not null,
  11353.     is_long            bit        not null,
  11354.     auto_unique_value    tinyint        not null,
  11355.     data_precision        int         null,
  11356.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  11357.     column_size        int         null,
  11358.     literal_prefix        nvarchar(32)    null,
  11359.     literal_suffix        nvarchar(32)     null,
  11360.     searchable        int        not null,
  11361.     unsigned_attribute    tinyint        null,
  11362.     local_type_name     sysname        null
  11363.     )
  11364. end
  11365. go
  11366.  
  11367. grant select on spt_provider_types to public
  11368. go
  11369. dump tran master with no_log
  11370. go
  11371.  
  11372. /*
  11373. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  11374. */
  11375. begin tran
  11376.  
  11377. /* Get case sensitivity */
  11378. declare @case_sensitive bit
  11379. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  11380. /* Local Char */
  11381. insert into spt_provider_types values
  11382.     (
  11383.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  11384.     0,                /* fixlen */
  11385.     'char',                /* type_name */
  11386.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  11387.     0,                /* best_match */
  11388.     null,                /* is_nullable */
  11389.     @case_sensitive,        /* case_sensitive */
  11390.     0,                /* fixed_prec_scale */
  11391.     0,                /* is_long */
  11392.     0,                /* auto_unique_value */
  11393.     null,                /* data_precision */
  11394.     null,                /* numeric_scale */
  11395.     null,                /* column_size */
  11396.     '''',                /* literal_prefix */
  11397.     '''',                /* literal_suffix */
  11398.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11399.     null,                /* unsigned_attribute */
  11400.     'char'                /* local_type_name */
  11401.     )
  11402.  
  11403. /* Local Varchar */
  11404. insert into spt_provider_types values
  11405.     (
  11406.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  11407.     null,                /* fixlen */
  11408.     'varchar',            /* type_name */
  11409.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  11410.     1,                /* best_match */
  11411.     null,                /* is_nullable */
  11412.     @case_sensitive,        /* case_sensitive */
  11413.     0,                /* fixed_prec_scale */
  11414.     0,                /* is_long */
  11415.     0,                /* auto_unique_value */
  11416.     null,                /* data_precision */
  11417.     null,                /* numeric_scale */
  11418.     null,                /* column_size */
  11419.     '''',                /* literal_prefix */
  11420.     '''',                /* literal_suffix */
  11421.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11422.     null,                /* unsigned_attribute */
  11423.     'varchar'            /* local_type_name */
  11424.     )
  11425.  
  11426. /* Local Text */
  11427. insert into spt_provider_types values
  11428.     (
  11429.     35  /*SQLTEXT*/,        /* ss_dtype */
  11430.     null,                /* fixlen */
  11431.     'text',                /* type_name */
  11432.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  11433.     0,                /* best_match */
  11434.     null,                /* is_nullable */
  11435.     @case_sensitive,        /* case_sensitive */
  11436.     0,                /* fixed_prec_scale */
  11437.     1,                /* is_long */
  11438.     0,                /* auto_unique_value */
  11439.     null,                /* data_precision */
  11440.     null,                /* numeric_scale */
  11441.     2147483647,            /* column_size */
  11442.     '''',                /* literal_prefix */
  11443.     '''',                /* literal_suffix */
  11444.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  11445.     null,                /* unsigned_attribute */
  11446.     'text'                /* local_type_name */
  11447.     )
  11448. commit tran
  11449. go
  11450. dump tran master with no_log
  11451. go
  11452.  
  11453. /*
  11454. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  11455. */
  11456. begin tran
  11457. /* Local Binary */
  11458. insert into spt_provider_types values
  11459.     (
  11460.     45 /*SQLBINARY*/,        /* ss_dtype */
  11461.     0,                /* fixlen */
  11462.     'binary',            /* type_name */
  11463.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  11464.     0,                /* best_match */
  11465.     null,                /* is_nullable */
  11466.     0,                /* case_sensitive */
  11467.     0,                /* fixed_prec_scale */
  11468.     0,                /* is_long */
  11469.     0,                /* auto_unique_value */
  11470.     null,                /* data_precision */
  11471.     null,                /* numeric_scale */
  11472.     null,                /* column_size */
  11473.     '0x',                /* literal_prefix */
  11474.     null,                /* literal_suffix */
  11475.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11476.     null,                /* unsigned_attribute */
  11477.     'binary'            /* local_type_name */
  11478.     )
  11479.  
  11480. /* Local Varbinary */
  11481. insert into spt_provider_types values
  11482.     (
  11483.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  11484.     null,                /* fixlen */
  11485.     'varbinary',            /* type_name */
  11486.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  11487.     1,                /* best_match */
  11488.     null,                /* is_nullable */
  11489.     0,                /* case_sensitive */
  11490.     0,                /* fixed_prec_scale */
  11491.     0,                /* is_long */
  11492.     0,                /* auto_unique_value */
  11493.     null,                /* data_precision */
  11494.     null,                /* numeric_scale */
  11495.     null,                /* column_size */
  11496.     '0x',                /* literal_prefix */
  11497.     null,                /* literal_suffix */
  11498.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11499.     null,                /* unsigned_attribute */
  11500.     'varbinary'            /* local_type_name */
  11501.     )
  11502.  
  11503. /* Local Image */
  11504. insert into spt_provider_types values
  11505.     (
  11506.     34 /*SQLIMAGE*/,        /* ss_dtype */
  11507.     null,                /* fixlen */
  11508.     'image',            /* type_name */
  11509.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  11510.     0,                /* best_match */
  11511.     null,                /* is_nullable */
  11512.     0,                /* case_sensitive */
  11513.     0,                /* fixed_prec_scale */
  11514.     1,                /* is_long */
  11515.     0,                /* auto_unique_value */
  11516.     null,                /* data_precision */
  11517.     null,                /* numeric_scale */
  11518.     2147483647,            /* column_size */
  11519.     '0x',                /* literal_prefix */
  11520.     null,                /* literal_suffix */
  11521.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  11522.     null,                /* unsigned_attribute */
  11523.     'image'                /* local_type_name */
  11524.     )
  11525.  
  11526. commit tran
  11527. go
  11528. dump tran master with no_log
  11529. go
  11530.  
  11531. /*
  11532. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  11533. */
  11534. begin tran
  11535.  
  11536. /* Local Datetime */
  11537. insert into spt_provider_types values
  11538.     (
  11539.     61 /*SQLDATETIME*/,        /* ss_dtype */
  11540.     8,                /* fixlen */
  11541.     'datetime',            /* type_name */
  11542.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11543.     1,                /* best_match */
  11544.     null,                /* is_nullable */
  11545.     0,                /* case_sensitive */
  11546.     0,                /* fixed_prec_scale */
  11547.     0,                /* is_long */
  11548.     0,                /* auto_unique_value */
  11549.     23,                /* data_precision */
  11550.     null,                /* numeric_scale */
  11551.     null,                /* column_size */
  11552.     '''',                /* literal_prefix */
  11553.     '''',                /* literal_suffix */
  11554.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11555.     null,                /* unsigned_attribute */
  11556.     'datetime'            /* local_type_name */
  11557.     )
  11558.  
  11559. /* Local Smalldatetime */
  11560. insert into spt_provider_types values
  11561.     (
  11562.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  11563.     4,                /* fixlen */
  11564.     'smalldatetime',        /* type_name */
  11565.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11566.     0,                /* best_match */
  11567.     null,                /* is_nullable */
  11568.     0,                /* case_sensitive */
  11569.     0,                /* fixed_prec_scale */
  11570.     0,                /* is_long */
  11571.     0,                /* auto_unique_value */
  11572.     16,                /* data_precision */
  11573.     null,                /* numeric_scale */
  11574.     null,                /* column_size */
  11575.     '''',                /* literal_prefix */
  11576.     '''',                /* literal_suffix */
  11577.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11578.     null,                /* unsigned_attribute */
  11579.     'smalldatetime'            /* local_type_name */
  11580.     )
  11581.  
  11582. if (charindex('7.00', @@version) = 0 and
  11583.     charindex('8.00', @@version) = 0)
  11584.     begin    /*    Add nullable type for non-Sphinx server */
  11585.     
  11586.     /* Local Datetimn */
  11587.     insert into spt_provider_types values
  11588.         (
  11589.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11590.         4,                /* fixlen */
  11591.         'smalldatetime',        /* type_name */
  11592.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11593.         0,                /* best_match */
  11594.         null,                /* is_nullable */
  11595.         0,                /* case_sensitive */
  11596.         0,                /* fixed_prec_scale */
  11597.         0,                /* is_long */
  11598.         0,                /* auto_unique_value */
  11599.         16,                /* data_precision */
  11600.         null,                /* numeric_scale */
  11601.         null,                /* column_size */
  11602.         '''',                /* literal_prefix */
  11603.         '''',                /* literal_suffix */
  11604.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11605.         null,                /* unsigned_attribute */
  11606.         'smalldatetime'            /* local_type_name */
  11607.         )    
  11608.     insert into spt_provider_types values
  11609.         (
  11610.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11611.         8,                /* fixlen */
  11612.         'datetime',            /* type_name */
  11613.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11614.         0,                /* best_match */
  11615.         null,                /* is_nullable */
  11616.         0,                /* case_sensitive */
  11617.         0,                /* fixed_prec_scale */
  11618.         0,                /* is_long */
  11619.         0,                /* auto_unique_value */
  11620.         23,                /* data_precision */
  11621.         null,                /* numeric_scale */
  11622.         null,                /* column_size */
  11623.         '''',                /* literal_prefix */
  11624.         '''',                /* literal_suffix */
  11625.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11626.         null,                /* unsigned_attribute */
  11627.         'datetime'            /* local_type_name */
  11628.         )
  11629.     end
  11630.  
  11631. commit tran
  11632. go
  11633. dump tran master with no_log
  11634. go
  11635.  
  11636. /*
  11637. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  11638. */
  11639. begin tran
  11640.  
  11641. /* Local Smallmoney */
  11642. insert into spt_provider_types values
  11643.     (
  11644.     122 /*SQLMONEY4*/,        /* ss_dtype */
  11645.     4,                /* fixlen */
  11646.     'smallmoney',            /* type_name */
  11647.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11648.     0,                /* best_match */
  11649.     null,                /* is_nullable */
  11650.     0,                /* case_sensitive */
  11651.     1,                /* fixed_prec_scale */
  11652.     0,                /* is_long */
  11653.     0,                /* auto_unique_value */
  11654.     10,                /* data_precision */
  11655.     null,                /* numeric_scale */
  11656.     null,                /* column_size */
  11657.     '$',                /* literal_prefix */
  11658.     null,                /* literal_suffix */
  11659.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11660.     0,                /* unsigned_attribute */
  11661.     'smallmoney'            /* local_type_name */
  11662.     )    
  11663.     
  11664. /* Local Money */
  11665. insert into spt_provider_types values
  11666.     (
  11667.     60 /*SQLMONEY*/,        /* ss_dtype */
  11668.     8,                /* fixlen */
  11669.     'money',            /* type_name */
  11670.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11671.     1,                /* best_match */
  11672.     null,                /* is_nullable */
  11673.     0,                /* case_sensitive */
  11674.     1,                /* fixed_prec_scale */
  11675.     0,                /* is_long */
  11676.     0,                /* auto_unique_value */
  11677.     19,                /* data_precision */
  11678.     null,                /* numeric_scale */
  11679.     null,                /* column_size */
  11680.     '$',                /* literal_prefix */
  11681.     null,                /* literal_suffix */
  11682.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11683.     0,                /* unsigned_attribute */
  11684.     'money'                /* local_type_name */
  11685.     )    
  11686.  
  11687. if (charindex('7.00', @@version) = 0 and
  11688.     charindex('8.00', @@version) = 0)
  11689.     begin    /*    Add nullable type for non-Sphinx server */
  11690.  
  11691.     /* Local Moneyn */
  11692.     insert into spt_provider_types values
  11693.         (
  11694.         110 /*SQLMONEYN*/,    /* ss_dtype */
  11695.         4,            /* fixlen */
  11696.         'smallmoney',        /* type_name */
  11697.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11698.         0,            /* best_match */
  11699.         null,            /* is_nullable */
  11700.         0,            /* case_sensitive */
  11701.         1,            /* fixed_prec_scale */
  11702.         0,            /* is_long */
  11703.         0,            /* auto_unique_value */
  11704.         10,            /* data_precision */
  11705.         null,            /* numeric_scale */
  11706.         null,            /* column_size */
  11707.         '$',            /* literal_prefix */
  11708.         null,            /* literal_suffix */
  11709.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11710.         0,            /* unsigned_attribute */
  11711.         'smallmoney'        /* local_type_name */
  11712.         )    
  11713.     insert into spt_provider_types values
  11714.         (
  11715.         110 /*SQLMONEYN*/,    /* ss_dtype */
  11716.         8,            /* fixlen */
  11717.         'money',        /* type_name */
  11718.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11719.         0,            /* best_match */
  11720.         null,            /* is_nullable */
  11721.         0,            /* case_sensitive */
  11722.         1,            /* fixed_prec_scale */
  11723.         0,            /* is_long */
  11724.         0,            /* auto_unique_value */
  11725.         19,            /* data_precision */
  11726.         null,            /* numeric_scale */
  11727.         null,            /* column_size */
  11728.         '$',            /* literal_prefix */
  11729.         null,            /* literal_suffix */
  11730.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11731.         null,            /* unsigned_attribute */
  11732.         'money'            /* local_type_name */
  11733.         )    
  11734.     end
  11735.  
  11736. commit tran
  11737. go
  11738. dump tran master with no_log
  11739. go
  11740.  
  11741. /*
  11742. ** Insert the spt_provider_types rows for the numeric data types
  11743. */
  11744. begin tran
  11745.  
  11746. /* Local Float */
  11747. insert into spt_provider_types values
  11748.     (
  11749.     62 /*SQLFLT8*/,            /* ss_dtype */
  11750.     8,                /* fixlen */
  11751.     'float',            /* type_name */
  11752.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  11753.     1,                /* best_match */
  11754.     null,                /* is_nullable */
  11755.     0,                /* case_sensitive */
  11756.     0,                /* fixed_prec_scale */
  11757.     0,                /* is_long */
  11758.     0,                /* auto_unique_value */
  11759.     15,                /* data_precision */
  11760.     null,                /* numeric_scale */
  11761.     null,                /* column_size */
  11762.     null,                /* literal_prefix */
  11763.     null,                /* literal_suffix */
  11764.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11765.     0,                /* unsigned_attribute */
  11766.     'float'                /* local_type_name */
  11767.     )    
  11768.  
  11769. /* Local Real */
  11770. insert into spt_provider_types values
  11771.     (
  11772.     59 /*SQLFLT4*/,            /* ss_dtype */
  11773.     4,                /* fixlen */
  11774.     'real',                /* type_name */
  11775.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  11776.     1,                /* best_match */
  11777.     null,                /* is_nullable */
  11778.     0,                /* case_sensitive */
  11779.     0,                /* fixed_prec_scale */
  11780.     0,                /* is_long */
  11781.     0,                /* auto_unique_value */
  11782.     7,                /* data_precision */
  11783.     null,                /* numeric_scale */
  11784.     null,                /* column_size */
  11785.     null,                /* literal_prefix */
  11786.     null,                /* literal_suffix */
  11787.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11788.     0,                /* unsigned_attribute */
  11789.     'real'                /* local_type_name */
  11790.     )    
  11791.  
  11792. /* Local Int */
  11793. insert into spt_provider_types values
  11794.     (
  11795.     56 /*SQLINT4*/,            /* ss_dtype */
  11796.     4,                /* fixlen */
  11797.     'int',                /* type_name */
  11798.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  11799.     1,                /* best_match */
  11800.     null,                /* is_nullable */
  11801.     0,                /* case_sensitive */
  11802.     1,                /* fixed_prec_scale */
  11803.     0,                /* is_long */
  11804.     1,                /* auto_unique_value */
  11805.     10,                /* data_precision */
  11806.     null,                /* numeric_scale */
  11807.     null,                /* column_size */
  11808.     null,                /* literal_prefix */
  11809.     null,                /* literal_suffix */
  11810.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11811.     0,                /* unsigned_attribute */
  11812.     'int'                /* local_type_name */
  11813.     )    
  11814.  
  11815. /* Local Smallint */
  11816. insert into spt_provider_types values
  11817.     (
  11818.     52 /*SQLINT2*/,            /* ss_dtype */
  11819.     2,                /* fixlen */
  11820.     'smallint',            /* type_name */
  11821.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  11822.     1,                /* best_match */
  11823.     null,                /* is_nullable */
  11824.     0,                /* case_sensitive */
  11825.     1,                /* fixed_prec_scale */
  11826.     0,                /* is_long */
  11827.     1,                /* auto_unique_value */
  11828.     5,                /* data_precision */
  11829.     null,                /* numeric_scale */
  11830.     null,                /* column_size */
  11831.     null,                /* literal_prefix */
  11832.     null,                /* literal_suffix */
  11833.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11834.     0,                /* unsigned_attribute */
  11835.     'smallint'            /* local_type_name */
  11836.     )    
  11837.  
  11838. /* Local Tinyint */
  11839. insert into spt_provider_types values
  11840.     (
  11841.     48 /*SQLINT1*/,            /* ss_dtype */
  11842.     1,                /* fixlen */
  11843.     'tinyint',            /* type_name */
  11844.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  11845.     1,                /* best_match */
  11846.     null,                /* is_nullable */
  11847.     0,                /* case_sensitive */
  11848.     1,                /* fixed_prec_scale */
  11849.     0,                /* is_long */
  11850.     1,                /* auto_unique_value */
  11851.     3,                /* data_precision */
  11852.     null,                /* numeric_scale */
  11853.     null,                /* column_size */
  11854.     null,                /* literal_prefix */
  11855.     null,                /* literal_suffix */
  11856.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11857.     1,                /* unsigned_attribute */
  11858.     'tinyint'            /* local_type_name */
  11859.     )    
  11860. commit tran
  11861. go
  11862. dump tran master with no_log
  11863. go
  11864. begin tran    
  11865. if (charindex('6.00', @@version) > 0 or
  11866.     charindex('6.50', @@version) > 0 or
  11867.     charindex('7.00', @@version) > 0 or
  11868.     charindex('8.00', @@version) > 0)
  11869.     begin    /*    Add 6.0 data types */
  11870.     
  11871.     /* Local Decimal */
  11872.     insert into spt_provider_types values
  11873.         (
  11874.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  11875.         0,                /* fixlen */
  11876.         'decimal',            /* type_name */
  11877.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  11878.         0,                /* best_match */
  11879.         null,                /* is_nullable */
  11880.         0,                /* case_sensitive */
  11881.         1,                /* fixed_prec_scale */
  11882.         0,                /* is_long */
  11883.         1,                /* auto_unique_value */
  11884.         38,                /* data_precision */
  11885.         null,                /* numeric_scale */
  11886.         null,                /* column_size */
  11887.         null,                /* literal_prefix */
  11888.         null,                /* literal_suffix */
  11889.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11890.         0,                /* unsigned_attribute */
  11891.         'decimal'            /* local_type_name */
  11892.         )    
  11893.  
  11894.     /* Local Numeric */
  11895.     insert into spt_provider_types values
  11896.         (
  11897.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  11898.         0,                /* fixlen */
  11899.         'numeric',            /* type_name */
  11900.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  11901.         1,                /* best_match */
  11902.         null,                /* is_nullable */
  11903.         0,                /* case_sensitive */
  11904.         1,                /* fixed_prec_scale */
  11905.         0,                /* is_long */
  11906.         1,                /* auto_unique_value */
  11907.         38,                /* data_precision */
  11908.         null,                /* numeric_scale */
  11909.         null,                /* column_size */
  11910.         null,                /* literal_prefix */
  11911.         null,                /* literal_suffix */
  11912.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11913.         0,                /* unsigned_attribute */
  11914.         'numeric'            /* local_type_name */
  11915.         )    
  11916.     end
  11917. commit tran
  11918. go
  11919. dump tran master with no_log
  11920. go
  11921. begin tran
  11922. if (charindex('7.00', @@version) = 0 and
  11923.     charindex('8.00', @@version) = 0)
  11924.     begin    /*    Add nullable type for non-Sphinx server */
  11925.     
  11926.     /* Local Floatn */
  11927.     insert into spt_provider_types values
  11928.         (
  11929.         109 /*SQLFLTN*/,        /* ss_dtype */
  11930.         8,                /* fixlen */
  11931.         'float',            /* type_name */
  11932.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  11933.         0,                /* best_match */
  11934.         null,                /* is_nullable */
  11935.         0,                /* case_sensitive */
  11936.         0,                /* fixed_prec_scale */
  11937.         0,                /* is_long */
  11938.         0,                /* auto_unique_value */
  11939.         15,                /* data_precision */
  11940.         null,                /* numeric_scale */
  11941.         null,                /* column_size */
  11942.         null,                /* literal_prefix */
  11943.         null,                /* literal_suffix */
  11944.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11945.         null,                /* unsigned_attribute */
  11946.         'float'                /* local_type_name */
  11947.         )    
  11948.     insert into spt_provider_types values
  11949.         (
  11950.         109 /*SQLFLT4*/,        /* ss_dtype */
  11951.         4,                /* fixlen */
  11952.         'real',                /* type_name */
  11953.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  11954.         0,                /* best_match */
  11955.         null,                /* is_nullable */
  11956.         0,                /* case_sensitive */
  11957.         0,                /* fixed_prec_scale */
  11958.         0,                /* is_long */
  11959.         0,                /* auto_unique_value */
  11960.         7,                /* data_precision */
  11961.         null,                /* numeric_scale */
  11962.         null,                /* column_size */
  11963.         null,                /* literal_prefix */
  11964.         null,                /* literal_suffix */
  11965.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11966.         null,                /* unsigned_attribute */
  11967.         'real'                /* local_type_name */
  11968.         )    
  11969.  
  11970.     /* Local Intn */
  11971.     insert into spt_provider_types values
  11972.         (
  11973.         38 /*SQLINTN*/,            /* ss_dtype */
  11974.         4,                /* fixlen */
  11975.         'int',                /* type_name */
  11976.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  11977.         1,                /* best_match */
  11978.         null,                /* is_nullable */
  11979.         0,                /* case_sensitive */
  11980.         1,                /* fixed_prec_scale */
  11981.         0,                /* is_long */
  11982.         1,                /* auto_unique_value */
  11983.         10,                /* data_precision */
  11984.         null,                /* numeric_scale */
  11985.         null,                /* column_size */
  11986.         null,                /* literal_prefix */
  11987.         null,                /* literal_suffix */
  11988.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11989.         0,                /* unsigned_attribute */
  11990.         'int'                /* local_type_name */
  11991.         )    
  11992.     insert into spt_provider_types values
  11993.         (
  11994.         38 /*SQLINTN*/,            /* ss_dtype */
  11995.         2,                /* fixlen */
  11996.         'smallint',            /* type_name */
  11997.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  11998.         0,                /* best_match */
  11999.         null,                /* is_nullable */
  12000.         0,                /* case_sensitive */
  12001.         1,                /* fixed_prec_scale */
  12002.         0,                /* is_long */
  12003.         1,                /* auto_unique_value */
  12004.         5,                /* data_precision */
  12005.         null,                /* numeric_scale */
  12006.         null,                /* column_size */
  12007.         null,                /* literal_prefix */
  12008.         null,                /* literal_suffix */
  12009.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12010.         0,                /* unsigned_attribute */
  12011.         'smallint'            /* local_type_name */
  12012.         )    
  12013.     insert into spt_provider_types values
  12014.         (
  12015.         38 /*SQLINTN*/,            /* ss_dtype */
  12016.         1,                /* fixlen */
  12017.         'tinyint',            /* type_name */
  12018.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  12019.         0,                /* best_match */
  12020.         null,                /* is_nullable */
  12021.         0,                /* case_sensitive */
  12022.         1,                /* fixed_prec_scale */
  12023.         0,                /* is_long */
  12024.         1,                /* auto_unique_value */
  12025.         3,                /* data_precision */
  12026.         null,                /* numeric_scale */
  12027.         null,                /* column_size */
  12028.         null,                /* literal_prefix */
  12029.         null,                /* literal_suffix */
  12030.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12031.         1,                /* unsigned_attribute */
  12032.         'tinyint'            /* local_type_name */
  12033.         )    
  12034.  
  12035.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  12036.         begin    /*    Add 6.0 data types */
  12037.         
  12038.         /* Local Decimaln */
  12039.         insert into spt_provider_types values
  12040.             (
  12041.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  12042.             0,                /* fixlen */
  12043.             'decimal',            /* type_name */
  12044.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  12045.             0,                /* best_match */
  12046.             null,                /* is_nullable */
  12047.             0,                /* case_sensitive */
  12048.             1,                /* fixed_prec_scale */
  12049.             0,                /* is_long */
  12050.             1,                /* auto_unique_value */
  12051.             38,                /* data_precision */
  12052.             null,                /* numeric_scale */
  12053.             null,                /* column_size */
  12054.             null,                /* literal_prefix */
  12055.             null,                /* literal_suffix */
  12056.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12057.             0,                /* unsigned_attribute */
  12058.             'decimal'            /* local_type_name */
  12059.             )    
  12060.  
  12061.         /* Local Numericn */
  12062.         insert into spt_provider_types values
  12063.             (
  12064.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  12065.             0,                /* fixlen */
  12066.             'numeric',            /* type_name */
  12067.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  12068.             0,                /* best_match */
  12069.             null,                /* is_nullable */
  12070.             0,                /* case_sensitive */
  12071.             1,                /* fixed_prec_scale */
  12072.             0,                /* is_long */
  12073.             1,                /* auto_unique_value */
  12074.             38,                /* data_precision */
  12075.             null,                /* numeric_scale */
  12076.             null,                /* column_size */
  12077.             null,                /* literal_prefix */
  12078.             null,                /* literal_suffix */
  12079.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12080.             0,                /* unsigned_attribute */
  12081.             'numeric'            /* local_type_name */
  12082.             )    
  12083.         end
  12084.     end
  12085. commit tran
  12086. go
  12087. dump tran master with no_log
  12088. go
  12089.  
  12090. /*
  12091. **    Remaining data types
  12092. */
  12093. if (charindex('7.00', @@version) = 0 and
  12094.     charindex('8.00', @@version) = 0)
  12095. begin
  12096.     print ''
  12097.     print ''
  12098.     print 'Warning:'
  12099.     print 'you are installing the stored procedures '
  12100.     print 'on a pre 8.0 SQL Server.'
  12101.     print 'Ignore the following errors.'
  12102. end
  12103. go
  12104. begin tran
  12105.  
  12106. if (charindex('7.00', @@version) > 0 or
  12107.     charindex('8.00', @@version) > 0)
  12108.     begin
  12109.     /* Local Bit */
  12110.     insert into spt_provider_types values
  12111.         (
  12112.         50 /*SQLBIT*/,            /* ss_dtype */
  12113.         0,                /* fixlen */
  12114.         'bit',                /* type_name */
  12115.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  12116.         1,                /* best_match */
  12117.         null,                /* is_nullable */
  12118.         0,                /* case_sensitive */
  12119.         0,                /* fixed_prec_scale */
  12120.         0,                /* is_long */
  12121.         0,                /* auto_unique_value */
  12122.         1,                /* data_precision */
  12123.         null,                /* numeric_scale */
  12124.         null,                /* column_size */
  12125.         null,                /* literal_prefix */
  12126.         null,                /* literal_suffix */
  12127.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12128.         null,                /* unsigned_attribute */
  12129.         'bit'                /* local_type_name */
  12130.         )        
  12131.     
  12132.     /* Local Timestamp */
  12133.     insert into spt_provider_types values
  12134.         (
  12135.         0,                /* ss_dtype */
  12136.         8,                /* fixlen */
  12137.         'timestamp',            /* type_name */
  12138.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  12139.         0,                /* best_match */
  12140.         null,                /* is_nullable */
  12141.         0,                /* case_sensitive */
  12142.         0,                /* fixed_prec_scale */
  12143.         0,                /* is_long */
  12144.         0,                /* auto_unique_value */
  12145.         null,                /* data_precision */
  12146.         null,                /* numeric_scale */
  12147.         null,                /* column_size */
  12148.         '0x',                /* literal_prefix */
  12149.         null,                /* literal_suffix */
  12150.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12151.         null,                /* unsigned_attribute */
  12152.         'timestamp'            /* local_type_name */
  12153.         )    
  12154.     /* Local GUID */
  12155.     insert into spt_provider_types values
  12156.         (
  12157.         0,                /* ss_dtype */
  12158.         16,                /* fixlen */
  12159.         'uniqueidentifier',        /* type_name */
  12160.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  12161.         1,                /* best_match */
  12162.         null,                /* is_nullable */
  12163.         0,                /* case_sensitive */
  12164.         0,                /* fixed_prec_scale */
  12165.         0,                /* is_long */
  12166.         0,                /* auto_unique_value */
  12167.         null,                /* data_precision */
  12168.         null,                /* numeric_scale */
  12169.         null,                /* column_size */
  12170.         '''',                /* literal_prefix */
  12171.         '''',                /* literal_suffix */
  12172.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12173.         null,                /* unsigned_attribute */
  12174.         'uniqueidentifier'        /* local_type_name */
  12175.         )    
  12176.     
  12177.     /* Get case sensitivity */
  12178.     declare @ncase_sensitive bit
  12179.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12180.  
  12181.     /* Local NChar */
  12182.     insert into spt_provider_types values
  12183.         (
  12184.         0,                /* ss_dtype */
  12185.         0,                /* fixlen */
  12186.         'nchar',            /* type_name */
  12187.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12188.         0,                /* best_match */
  12189.         null,                /* is_nullable */
  12190.         @ncase_sensitive,        /* case_sensitive */
  12191.         0,                /* fixed_prec_scale */
  12192.         0,                /* is_long */
  12193.         0,                /* auto_unique_value */
  12194.         null,                /* data_precision */
  12195.         null,                /* numeric_scale */
  12196.         null,                /* column_size */
  12197.         'N''',                /* literal_prefix */
  12198.         '''',                /* literal_suffix */
  12199.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12200.         null,                /* unsigned_attribute */
  12201.         'nchar'                /* local_type_name */
  12202.         )    
  12203.  
  12204.     /* Local NVarChar */
  12205.     insert into spt_provider_types values
  12206.         (
  12207.         0,                /* ss_dtype */
  12208.         null,                /* fixlen */
  12209.         'nvarchar',            /* type_name */
  12210.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12211.         1,                /* best_match */
  12212.         null,                /* is_nullable */
  12213.         @ncase_sensitive,        /* case_sensitive */
  12214.         0,                /* fixed_prec_scale */
  12215.         0,                /* is_long */
  12216.         0,                /* auto_unique_value */
  12217.         null,                /* data_precision */
  12218.         null,                /* numeric_scale */
  12219.         null,                /* column_size */
  12220.         'N''',                /* literal_prefix */
  12221.         '''',                /* literal_suffix */
  12222.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12223.         null,                /* unsigned_attribute */
  12224.         'nvarchar'            /* local_type_name */
  12225.         )    
  12226.  
  12227.     /* Local NText */
  12228.     insert into spt_provider_types values
  12229.         (
  12230.         0,                /* ss_dtype */
  12231.         null,                /* fixlen */
  12232.         'ntext',            /* type_name */
  12233.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12234.         0,                /* best_match */
  12235.         null,                /* is_nullable */
  12236.         @ncase_sensitive,        /* case_sensitive */
  12237.         0,                /* fixed_prec_scale */
  12238.         1,                /* is_long */
  12239.         0,                /* auto_unique_value */
  12240.         null,                /* data_precision */
  12241.         null,                /* numeric_scale */
  12242.         1073741823,            /* column_size */
  12243.         'N''',                /* literal_prefix */
  12244.         '''',                /* literal_suffix */
  12245.         2 /*DB_LIKE_ONLY*/,        /* searchable */
  12246.         null,                /* unsigned_attribute */
  12247.         'ntext'                /* local_type_name */
  12248.         )    
  12249.  
  12250.     if (charindex('8.00', @@version) > 0)
  12251.     begin
  12252.     /* Local BIGINT */
  12253.     insert into spt_provider_types values
  12254.         (
  12255.         127,                /* ss_dtype */
  12256.         8,                /* fixlen */
  12257.         'bigint',            /* type_name */
  12258.         20 /*DBTYPE_I8*/,        /* oledb_data_type */
  12259.         1,                /* best_match */
  12260.         null,                /* is_nullable */
  12261.         0,                /* case_sensitive */
  12262.         1,                /* fixed_prec_scale */
  12263.         0,                /* is_long */
  12264.         1,                /* auto_unique_value */
  12265.         19,                /* data_precision */
  12266.         null,            /* numeric_scale */
  12267.         null,            /* column_size */
  12268.         null,            /* literal_prefix */
  12269.         null,            /* literal_suffix */
  12270.         3 /*DB_ALL_EXCEPT_LIKE*/,        /* searchable */
  12271.         0,                /* unsigned_attribute */
  12272.         'bigint'        /* local_type_name */
  12273.         )
  12274.  
  12275.     /* sql_variant */
  12276.     insert into spt_provider_types values
  12277.         (
  12278.         98,                /* ss_dtype */
  12279.         null,            /* fixlen */
  12280.         'sql_variant',    /* type_name */
  12281.         12 /*DBTYPE_VARIANT*/,        /* oledb_data_type */
  12282.         1,                /* best_match */
  12283.         null,            /* is_nullable */
  12284.         0,                /* case_sensitive */
  12285.         0,                /* fixed_prec_scale */
  12286.         0,                /* is_long */
  12287.         0,                /* auto_unique_value */
  12288.         16,                /* data_precision sizeof(VARIANT)*/
  12289.         null,            /* numeric_scale */
  12290.         null,            /* column_size */
  12291.         null,            /* literal_prefix */
  12292.         null,            /* literal_suffix */
  12293.         3 ,                    /*DB_ALL_EXCEPT_LIKE*/
  12294.         null,            /* unsigned_attribute */
  12295.         'sql_variant'    /* local_type_name */
  12296.         )
  12297.     end
  12298.  
  12299.  
  12300.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  12301.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  12302.  
  12303.     end
  12304. commit tran
  12305. go
  12306. dump tran master with no_log
  12307. go
  12308. begin tran
  12309. if (charindex('7.00', @@version) = 0 and
  12310.     charindex('8.00', @@version) = 0)
  12311.     begin
  12312.     /* Local Bit */
  12313.     insert into spt_provider_types values
  12314.         (
  12315.         50 /*SQLBIT*/,            /* ss_dtype */
  12316.         0,                /* fixlen */
  12317.         'bit',                /* type_name */
  12318.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  12319.         1,                /* best_match */
  12320.         0,                /* is_nullable */
  12321.         0,                /* case_sensitive */
  12322.         0,                /* fixed_prec_scale */
  12323.         0,                /* is_long */
  12324.         0,                /* auto_unique_value */
  12325.         1,                /* data_precision */
  12326.         null,                /* numeric_scale */
  12327.         null,                /* column_size */
  12328.         null,                /* literal_prefix */
  12329.         null,                /* literal_suffix */
  12330.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12331.         null,                /* unsigned_attribute */
  12332.         'bit'                /* local_type_name */
  12333.         )    
  12334.  
  12335.     /* Local Timestamp */
  12336.     insert into spt_provider_types values
  12337.         (
  12338.         45 /*SQLBINARY*/,        /* ss_dtype */
  12339.         8,                /* fixlen */
  12340.         'timestamp',            /* type_name */
  12341.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  12342.         0,                /* best_match */
  12343.         null,                /* is_nullable */
  12344.         0,                /* case_sensitive */
  12345.         0,                /* fixed_prec_scale */
  12346.         0,                /* is_long */
  12347.         0,                /* auto_unique_value */
  12348.         null,                /* data_precision */
  12349.         null,                /* numeric_scale */
  12350.         null,                /* column_size */
  12351.         '0x',                /* literal_prefix */
  12352.         null,                /* literal_suffix */
  12353.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12354.         null,                /* unsigned_attribute */
  12355.         'timestamp'            /* local_type_name */
  12356.         )        
  12357. end
  12358.  
  12359. commit tran
  12360. go
  12361. dump tran master with no_log
  12362. go
  12363.  
  12364. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  12365. go
  12366.  
  12367. dump tran master with no_log
  12368. go
  12369. if (charindex('6.00', @@version) > 0)
  12370.     begin
  12371.     if (exists (select * from sysobjects
  12372.         where name = 'spt_provider_types' and type = 'U '))
  12373.         begin
  12374.         drop table spt_provider_types
  12375.         dump tran master with no_log
  12376.         end
  12377.     end
  12378. go
  12379.  
  12380. if (charindex('7.00', @@version) = 0 and
  12381.     charindex('8.00', @@version) = 0)
  12382. begin
  12383.     print ''
  12384.     print ''
  12385.     print 'Warning:'
  12386.     print 'you are installing the stored procedures '
  12387.     print 'on a pre 8.0 SQL Server.'
  12388.     print 'Ignore the following errors.'
  12389. end
  12390. go
  12391.  
  12392.  
  12393. /* 
  12394. ** (2/11/98) 
  12395. ** The following SP is shared by instcat.sql and the server to add information
  12396. ** related to server language collations. The server calls it at the end of
  12397. ** collation change to update the catalog with the new language collations
  12398. ** Everything in this SP should NOT use tempdb.  
  12399. */
  12400.  
  12401. create procedure sp_add_server_sortinfo
  12402. as
  12403. -- spt_server_info has unigue clustered index on attribute_id
  12404. delete spt_server_info where attribute_id in (16,18)
  12405.  
  12406. --insert into spt_server_info
  12407. --    select 18,'COLLATION_SEQ',
  12408. --        'charset='+t2.name+' sort_order='+t1.name
  12409. --        +' charset_num='+rtrim(convert(char(4),t1.csid))+
  12410. --        ' sort_order_num='+rtrim(convert(char(4),t1.id))
  12411. --    from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  12412. --    where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  12413. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12414. declare @case_sensitive bit
  12415. declare @ncase_sensitive bit
  12416. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12417. select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12418. select @sortid = value from sysconfigures where config = 1123
  12419. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  12420. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12421.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12422.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12423.         from syscharsets where id = @csid
  12424.  
  12425. insert into spt_server_info
  12426.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12427.  
  12428. if @case_sensitive = 1 /* If case sensitive server */
  12429. begin
  12430.     insert into spt_server_info
  12431.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  12432. end
  12433. else
  12434. begin
  12435.     insert into spt_server_info
  12436.         values (16,'IDENTIFIER_CASE','MIXED')
  12437. end
  12438.  
  12439.  
  12440. update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12441.     where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12442. update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12443.     where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12444. update spt_provider_types set case_sensitive = @case_sensitive 
  12445.     where oledb_data_type = 129 /*DBTYPE_STR*/
  12446. update spt_provider_types set case_sensitive = @ncase_sensitive
  12447.     where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12448. /* sp_add_server_sortinfo */
  12449. go  
  12450.  
  12451.  
  12452. if (charindex('7.00', @@version) > 0)
  12453. begin
  12454.     exec sp_add_server_sortinfo
  12455. end
  12456. go
  12457.  
  12458. -- 8.0 version of sp_add_server_sortinfo
  12459. --
  12460. create procedure sp_add_server_sortinfo75
  12461. as
  12462. -- spt_server_info has unigue clustered index on attribute_id
  12463.     delete spt_server_info where attribute_id in (16, 18)
  12464.  
  12465.     declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12466.     declare @case_sensitive bit
  12467.     declare @ncase_sensitive bit
  12468.     select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12469.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12470.  
  12471.     select @sortid = convert(int, ServerProperty('sqlsortorder')) 
  12472.     select @csid = convert(int, ServerProperty('sqlcharset')) 
  12473.     select @sortname = name from master.dbo.syscharsets where id = @sortid
  12474.  
  12475.     if @sortid = 0 -- Non-SQL Collations
  12476.         BEGIN
  12477.         select @attribute_value = 'charset=' 
  12478.             + name
  12479.             + ' collation=' + isnull(convert(sysname, ServerProperty('collation')), ' ')
  12480.             from syscharsets where id = @csid
  12481.         END
  12482.     else
  12483.         BEGIN
  12484.         select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12485.             + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12486.             ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12487.             from syscharsets where id = @csid
  12488.         END
  12489.  
  12490.     insert into spt_server_info
  12491.             values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12492.  
  12493.     if @case_sensitive = 1 /* If case sensitive server */
  12494.     begin
  12495.         insert into spt_server_info
  12496.             values (16,'IDENTIFIER_CASE','SENSITIVE')
  12497.     end
  12498.     else
  12499.     begin
  12500.         insert into spt_server_info
  12501.             values (16,'IDENTIFIER_CASE','MIXED')
  12502.     end
  12503.  
  12504.  
  12505.     update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12506.         where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12507.     update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12508.         where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12509.     update spt_provider_types set case_sensitive = @case_sensitive 
  12510.         where oledb_data_type = 129 /*DBTYPE_STR*/
  12511.     update spt_provider_types set case_sensitive = @ncase_sensitive
  12512.         where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12513. /* sp_add_server_info75 */
  12514. go 
  12515.  
  12516.  
  12517.  
  12518. if (charindex('8.00', @@version) > 0)
  12519. begin
  12520.     exec sp_add_server_sortinfo75
  12521. end
  12522. go
  12523.  
  12524. print ''
  12525. print 'creating sp_catalogs_rowset'
  12526. go
  12527.  
  12528. create procedure sp_catalogs_rowset
  12529.     (
  12530.     @catalog_name        varchar(255)
  12531.     )        
  12532. as
  12533.     select
  12534.         CATALOG_NAME    = name,
  12535.         DESCRIPTION    = convert(varchar(1),null)
  12536.     from     master.dbo.sysdatabases
  12537.     where    name = @catalog_name
  12538. go
  12539. dump tran master with no_log
  12540. go
  12541. create procedure sp_catalogs_rowset;2
  12542.     (
  12543.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  12544.     )
  12545. as
  12546.     select
  12547.         CATALOG_NAME    = name,
  12548.         DESCRIPTION    = convert(varchar(1),null)
  12549.     from     master.dbo.sysdatabases
  12550.     order by 1
  12551. go
  12552. dump tran master with no_log
  12553. go
  12554.  
  12555. if (charindex('7.00', @@version) = 0 and
  12556.     charindex('8.00', @@version) = 0)
  12557. begin
  12558.     print ''
  12559.     print ''
  12560.     print 'Warning:'
  12561.     print 'you are installing the stored procedures '
  12562.     print 'on a pre 8.0 SQL Server.'
  12563.     print 'Ignore the following errors.'
  12564. end
  12565. else
  12566.     drop proc sp_catalogs_rowset
  12567. go
  12568.  
  12569. /*    Procedure for 8.0 server */
  12570. create procedure sp_catalogs_rowset
  12571.     (
  12572.     @catalog_name        sysname
  12573.     )        
  12574. as
  12575.     select
  12576.         CATALOG_NAME    = name,
  12577.         DESCRIPTION    = convert(nvarchar(1),null)
  12578.     from     master.dbo.sysdatabases
  12579.     where    name = @catalog_name
  12580. go
  12581. dump tran master with no_log
  12582. go
  12583. create procedure sp_catalogs_rowset;2
  12584.     (
  12585.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  12586.     )
  12587. as
  12588.     select
  12589.         CATALOG_NAME    = name,
  12590.         DESCRIPTION    = convert(nvarchar(1),null)
  12591.     from     master.dbo.sysdatabases
  12592.     order by 1
  12593. go
  12594. dump tran master with no_log
  12595. go
  12596. create procedure sp_catalogs_rowset;5
  12597.     (
  12598.     @server_name    sysname,
  12599.     @catalog_name    sysname = NULL
  12600.     )
  12601. as
  12602.     select    CATALOG_NAME,
  12603.         DESCRIPTION
  12604.     from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
  12605.     order by 1
  12606. go
  12607.  
  12608. grant execute on sp_catalogs_rowset to public
  12609. go
  12610.  
  12611. dump tran master with no_log
  12612. go
  12613. if (charindex('6.00', @@version) > 0)
  12614.     begin
  12615.     if (exists (select * from sysobjects
  12616.             where name = 'sp_catalogs_rowset' and type = 'P '))
  12617.         begin
  12618.         drop procedure sp_catalogs_rowset
  12619.         dump tran master with no_log
  12620.         end
  12621.     end
  12622. go
  12623.  
  12624.  
  12625. print ''
  12626. print 'creating sp_column_privileges_rowset'
  12627. go
  12628.  
  12629. /*    Procedure for 6.0 and 6.5 server */
  12630. CREATE PROCEDURE sp_column_privileges_rowset
  12631.     (
  12632.     @table_name     varchar(255) = null,
  12633.     @table_schema    varchar(255) = null,
  12634.     @column_name    varchar(255) = null,
  12635.     @grantor    varchar(255) = null,
  12636.     @grantee    varchar(255) = null
  12637.     )
  12638. as
  12639. IF @table_name is not null
  12640.     BEGIN
  12641.     select
  12642.         GRANTOR        = user_name(p.grantor),
  12643.         GRANTEE        = user_name(u.uid),
  12644.         TABLE_CATALOG    = db_name(),
  12645.         TABLE_SCHEMA    = user_name(o.uid),
  12646.         TABLE_NAME    = o.name,
  12647.         COLUMN_NAME    = c.name,
  12648.         COLUMN_GUID    = convert(binary(16),null),
  12649.         COLUMN_PROPID    = convert(int,null),
  12650.         PRIVILEGE_TYPE    = convert(varchar(30),
  12651.                     case p.action
  12652.                     when 193 then 'SELECT'
  12653.                     when 195 then 'INSERT'
  12654.                     when 197 then 'UPDATE'
  12655.                     else 'REFERENCES'
  12656.                     end),
  12657.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12658.     from 
  12659.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12660.     where
  12661.         o.name = @table_name
  12662.     and     o.type in ('U','V','S')
  12663.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12664.     and    c.id = o.id
  12665.     and     (@column_name is null or @column_name = c.name)
  12666.     and     c.id = p.id
  12667.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12668.     and     case 
  12669.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12670.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12671.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12672.             end & v.high <> 0            /* permission applies to this column */
  12673.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12674.     and     v.type = 'P'
  12675.     and     v.number = c.colid
  12676.     and    (@grantee is null or @grantee = user_name(u.uid))
  12677.             /* expand groups */
  12678.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12679.     and     p.protecttype <> 206    /* only grant rows */
  12680.     and     p.action in (26,193,195,197)
  12681.     and     o.uid <> u.uid            /* no rows for owner */
  12682.     and     not exists (            /* exclude revoke'd privileges */
  12683.             select *
  12684.             from sysprotects p1
  12685.             where
  12686.                 p1.protecttype = 206
  12687.             and     p1.action = p.action
  12688.             and     p1.id = p.id
  12689.             and     p1.uid = u.uid
  12690.             and     case 
  12691.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12692.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12693.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12694.                 end & v.high <> 0)            /* permission applies to this column */
  12695.     union
  12696.     select    /*    Add rows for table owner */
  12697.         GRANTOR        = user_name(u.uid),
  12698.         GRANTEE        = user_name(o.uid),
  12699.         TABLE_CATALOG    = db_name(),
  12700.         TABLE_SCHEMA    = user_name(o.uid),
  12701.         TABLE_NAME    = o.name,
  12702.         COLUMN_NAME    = c.name,
  12703.         COLUMN_GUID    = convert(binary(16),null),
  12704.         COLUMN_PROPID    = convert(int,null),
  12705.         PRIVILEGE_TYPE    = convert(varchar(30),
  12706.                     case v.number
  12707.                     when 193 then 'SELECT'
  12708.                     when 195 then 'INSERT'
  12709.                     when 197 then 'UPDATE'
  12710.                     else 'REFERENCES'
  12711.                     end),
  12712.         IS_GRANTABLE    = convert(bit,1)    
  12713.     from 
  12714.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12715.     where
  12716.         o.name = @table_name
  12717.     and     o.type in ('U','V','S')
  12718.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12719.     and    (@grantee is null or @grantee = user_name(o.uid))
  12720.     and    c.id = o.id
  12721.     and     (@column_name is null or @column_name = c.name)
  12722.     and     u.uid = 1        /* grantor is dbo of database */
  12723.     and    (@grantor is null or @grantor = user_name(u.uid))
  12724.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12725.     and     v.number in (26,193,195,197)
  12726.     and     not exists (        /* exclude revoke'd privileges */
  12727.             select *
  12728.             from     sysprotects p1
  12729.             where    p1.protecttype = 206
  12730.             and     p1.action = v.number
  12731.             and     p1.id = o.id
  12732.             and     p1.uid = o.uid)
  12733.     order by 4, 5, 6, 9, 1, 2
  12734.     END
  12735. ELSE
  12736.     BEGIN
  12737.     select
  12738.         GRANTOR        = user_name(p.grantor),
  12739.         GRANTEE        = user_name(u.uid),
  12740.         TABLE_CATALOG    = db_name(),
  12741.         TABLE_SCHEMA    = user_name(o.uid),
  12742.         TABLE_NAME    = o.name,
  12743.         COLUMN_NAME    = c.name,
  12744.         COLUMN_GUID    = convert(binary(16),null),
  12745.         COLUMN_PROPID    = convert(int,null),
  12746.         PRIVILEGE_TYPE    = convert(varchar(30),
  12747.                     case p.action
  12748.                     when 193 then 'SELECT'
  12749.                     when 195 then 'INSERT'
  12750.                     when 197 then 'UPDATE'
  12751.                     else 'REFERENCES'
  12752.                     end),
  12753.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12754.     from 
  12755.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12756.     where
  12757.         o.type in ('U','V','S')
  12758.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12759.     and    c.id = o.id
  12760.     and     (@column_name is null or @column_name = c.name)
  12761.     and     c.id = p.id
  12762.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12763.     and     case 
  12764.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12765.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12766.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12767.             end & v.high <> 0            /* permission applies to this column */
  12768.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12769.     and     v.type = 'P'
  12770.     and     v.number = c.colid
  12771.     and    (@grantee is null or @grantee = user_name(u.uid))
  12772.             /* expand groups */
  12773.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12774.     and     p.protecttype <> 206    /* only grant rows */
  12775.     and     p.action in (26,193,195,197)
  12776.     and     o.uid <> u.uid            /* no rows for owner */
  12777.     and     not exists (            /* exclude revoke'd privileges */
  12778.             select *
  12779.             from sysprotects p1
  12780.             where
  12781.                 p1.protecttype = 206
  12782.             and     p1.action = p.action
  12783.             and     p1.id = p.id
  12784.             and     p1.uid = u.uid
  12785.             and     case 
  12786.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12787.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12788.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12789.                 end & v.high <> 0)            /* permission applies to this column */
  12790.     union
  12791.     select    /*    Add rows for table owner */
  12792.         GRANTOR        = user_name(u.uid),
  12793.         GRANTEE        = user_name(o.uid),
  12794.         TABLE_CATALOG    = db_name(),
  12795.         TABLE_SCHEMA    = user_name(o.uid),
  12796.         TABLE_NAME    = o.name,
  12797.         COLUMN_NAME    = c.name,
  12798.         COLUMN_GUID    = convert(binary(16),null),
  12799.         COLUMN_PROPID    = convert(int,null),
  12800.         PRIVILEGE_TYPE    = convert(varchar(30),
  12801.                     case v.number
  12802.                     when 193 then 'SELECT'
  12803.                     when 195 then 'INSERT'
  12804.                     when 197 then 'UPDATE'
  12805.                     else 'REFERENCES'
  12806.                     end),
  12807.         IS_GRANTABLE    = convert(bit,1)    
  12808.     from 
  12809.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12810.     where
  12811.          o.type in ('U','V','S')
  12812.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12813.     and    (@grantee is null or @grantee = user_name(o.uid))
  12814.     and    c.id = o.id
  12815.     and     (@column_name is null or @column_name = c.name)
  12816.     and     u.uid = 1        /* grantor is dbo of database */
  12817.     and    (@grantor is null or @grantor = user_name(u.uid))
  12818.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12819.     and     v.number in (26,193,195,197)
  12820.     and     not exists (        /* exclude revoke'd privileges */
  12821.             select *
  12822.             from     sysprotects p1
  12823.             where    p1.protecttype = 206
  12824.             and     p1.action = v.number
  12825.             and     p1.id = o.id
  12826.             and     p1.uid = o.uid)
  12827.     order by 4, 5, 6, 9, 1, 2
  12828.     END
  12829. go
  12830. dump tran master with no_log
  12831. go
  12832. CREATE PROCEDURE sp_column_privileges_rowset;2
  12833.     (
  12834.        @handle        int output,
  12835.        @scrollopt    int output,
  12836.     @ccopt        int output,
  12837.     @rows        int output,
  12838.     @table_name     varchar(255) = null,
  12839.     @table_schema    varchar(255) = null,
  12840.     @column_name    varchar(255) = null,
  12841.     @grantor    varchar(255) = null,
  12842.     @grantee    varchar(255) = null
  12843.     )
  12844. as
  12845.  
  12846. declare @ret int
  12847.  
  12848. SET NOCOUNT ON
  12849.     
  12850. create table #spcprivsrowset1
  12851.     (
  12852.     GRANTOR        sysname not null,
  12853.     GRANTEE        sysname not null,
  12854.     TABLE_CATALOG    sysname not null,
  12855.     TABLE_SCHEMA    sysname not null,
  12856.     TABLE_NAME    sysname not null,
  12857.     COLUMN_NAME    sysname not null,
  12858.     COLUMN_GUID    binary(16) null,
  12859.     COLUMN_PROPID    int null,
  12860.     PRIVILEGE_TYPE    sysname not null,
  12861.     IS_GRANTABLE    bit not null
  12862.     )
  12863.  
  12864. IF @table_name is not null
  12865.     BEGIN
  12866.     insert into #spcprivsrowset1
  12867.     select
  12868.         GRANTOR        = user_name(p.grantor),
  12869.         GRANTEE        = user_name(u.uid),
  12870.         TABLE_CATALOG    = db_name(),
  12871.         TABLE_SCHEMA    = user_name(o.uid),
  12872.         TABLE_NAME    = o.name,
  12873.         COLUMN_NAME    = c.name,
  12874.         COLUMN_GUID    = convert(binary(16),null),
  12875.         COLUMN_PROPID    = convert(int,null),
  12876.         PRIVILEGE_TYPE    = convert(varchar(30),
  12877.                     case p.action
  12878.                     when 193 then 'SELECT'
  12879.                     when 195 then 'INSERT'
  12880.                     when 197 then 'UPDATE'
  12881.                     else 'REFERENCES'
  12882.                     end),
  12883.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12884.     from 
  12885.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12886.     where
  12887.         o.name = @table_name
  12888.     and     o.type in ('U','V','S')
  12889.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12890.     and    c.id = o.id
  12891.     and     (@column_name is null or @column_name = c.name)
  12892.     and     c.id = p.id
  12893.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12894.     and     case 
  12895.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12896.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12897.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12898.             end & v.high <> 0            /* permission applies to this column */
  12899.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12900.     and     v.type = 'P'
  12901.     and     v.number = c.colid
  12902.     and    (@grantee is null or @grantee = user_name(u.uid))
  12903.             /* expand groups */
  12904.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12905.     and     p.protecttype <> 206    /* only grant rows */
  12906.     and     p.action in (26,193,195,197)
  12907.     and     o.uid <> u.uid            /* no rows for owner */
  12908.     and     not exists (            /* exclude revoke'd privileges */
  12909.             select *
  12910.             from sysprotects p1
  12911.             where
  12912.                 p1.protecttype = 206
  12913.             and     p1.action = p.action
  12914.             and     p1.id = p.id
  12915.             and     p1.uid = u.uid
  12916.             and     case 
  12917.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12918.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12919.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12920.                 end & v.high <> 0)            /* permission applies to this column */
  12921.     union
  12922.     select    /*    Add rows for table owner */
  12923.         GRANTOR        = user_name(u.uid),
  12924.         GRANTEE        = user_name(o.uid),
  12925.         TABLE_CATALOG    = db_name(),
  12926.         TABLE_SCHEMA    = user_name(o.uid),
  12927.         TABLE_NAME    = o.name,
  12928.         COLUMN_NAME    = c.name,
  12929.         COLUMN_GUID    = convert(binary(16),null),
  12930.         COLUMN_PROPID    = convert(int,null),
  12931.         PRIVILEGE_TYPE    = convert(varchar(30),
  12932.                     case v.number
  12933.                     when 193 then 'SELECT'
  12934.                     when 195 then 'INSERT'
  12935.                     when 197 then 'UPDATE'
  12936.                     else 'REFERENCES'
  12937.                     end),
  12938.         IS_GRANTABLE    = convert(bit,1)    
  12939.     from 
  12940.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12941.     where
  12942.         o.name = @table_name
  12943.     and     o.type in ('U','V','S')
  12944.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12945.     and    (@grantee is null or @grantee = user_name(o.uid))
  12946.     and    c.id = o.id
  12947.     and     (@column_name is null or @column_name = c.name)
  12948.     and     u.uid = 1        /* grantor is dbo of database */
  12949.     and    (@grantor is null or @grantor = user_name(u.uid))
  12950.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12951.     and     v.number in (26,193,195,197)
  12952.     and     not exists (        /* exclude revoke'd privileges */
  12953.             select *
  12954.             from     sysprotects p1
  12955.             where    p1.protecttype = 206
  12956.             and     p1.action = v.number
  12957.             and     p1.id = o.id
  12958.             and     p1.uid = o.uid)
  12959.     order by 4, 5, 6, 9, 1, 2
  12960.     END
  12961. ELSE
  12962.     BEGIN
  12963.     insert into #spcprivsrowset1
  12964.     select
  12965.         GRANTOR        = user_name(p.grantor),
  12966.         GRANTEE        = user_name(u.uid),
  12967.         TABLE_CATALOG    = db_name(),
  12968.         TABLE_SCHEMA    = user_name(o.uid),
  12969.         TABLE_NAME    = o.name,
  12970.         COLUMN_NAME    = c.name,
  12971.         COLUMN_GUID    = convert(binary(16),null),
  12972.         COLUMN_PROPID    = convert(int,null),
  12973.         PRIVILEGE_TYPE    = convert(varchar(30),
  12974.                     case p.action
  12975.                     when 193 then 'SELECT'
  12976.                     when 195 then 'INSERT'
  12977.                     when 197 then 'UPDATE'
  12978.                     else 'REFERENCES'
  12979.                     end),
  12980.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12981.     from 
  12982.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12983.     where
  12984.         o.type in ('U','V','S')
  12985.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12986.     and    c.id = o.id
  12987.     and     (@column_name is null or @column_name = c.name)
  12988.     and     c.id = p.id
  12989.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12990.     and     case 
  12991.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12992.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12993.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12994.             end & v.high <> 0            /* permission applies to this column */
  12995.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12996.     and     v.type = 'P'
  12997.     and     v.number = c.colid
  12998.     and    (@grantee is null or @grantee = user_name(u.uid))
  12999.             /* expand groups */
  13000.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  13001.     and     p.protecttype <> 206    /* only grant rows */
  13002.     and     p.action in (26,193,195,197)
  13003.     and     o.uid <> u.uid            /* no rows for owner */
  13004.     and     not exists (            /* exclude revoke'd privileges */
  13005.             select *
  13006.             from sysprotects p1
  13007.             where
  13008.                 p1.protecttype = 206
  13009.             and     p1.action = p.action
  13010.             and     p1.id = p.id
  13011.             and     p1.uid = u.uid
  13012.             and     case 
  13013.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13014.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13015.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13016.                 end & v.high <> 0)            /* permission applies to this column */
  13017.     union
  13018.     select    /*    Add rows for table owner */
  13019.         GRANTOR        = user_name(u.uid),
  13020.         GRANTEE        = user_name(o.uid),
  13021.         TABLE_CATALOG    = db_name(),
  13022.         TABLE_SCHEMA    = user_name(o.uid),
  13023.         TABLE_NAME    = o.name,
  13024.         COLUMN_NAME    = c.name,
  13025.         COLUMN_GUID    = convert(binary(16),null),
  13026.         COLUMN_PROPID    = convert(int,null),
  13027.         PRIVILEGE_TYPE    = convert(varchar(30),
  13028.                     case v.number
  13029.                     when 193 then 'SELECT'
  13030.                     when 195 then 'INSERT'
  13031.                     when 197 then 'UPDATE'
  13032.                     else 'REFERENCES'
  13033.                     end),
  13034.         IS_GRANTABLE    = convert(bit,1)    
  13035.     from 
  13036.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13037.     where
  13038.          o.type in ('U','V','S')
  13039.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13040.     and    (@grantee is null or @grantee = user_name(o.uid))
  13041.     and    c.id = o.id
  13042.     and     (@column_name is null or @column_name = c.name)
  13043.     and     u.uid = 1        /* grantor is dbo of database */
  13044.     and    (@grantor is null or @grantor = user_name(u.uid))
  13045.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13046.     and     v.number in (26,193,195,197)
  13047.     and     not exists (        /* exclude revoke'd privileges */
  13048.             select *
  13049.             from     sysprotects p1
  13050.             where    p1.protecttype = 206
  13051.             and     p1.action = v.number
  13052.             and     p1.id = o.id
  13053.             and     p1.uid = o.uid)
  13054.     order by 4, 5, 6, 9, 1, 2
  13055.     END
  13056.  
  13057. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  13058.     @scrollopt output, @ccopt output, @rows output
  13059.  
  13060. drop table #spcprivsrowset1
  13061.  
  13062. return isnull(@ret,0)
  13063. go
  13064. dump tran master with no_log
  13065. go
  13066. CREATE PROCEDURE sp_column_privileges_rowset;3
  13067. as
  13068.     select
  13069.         GRANTOR        = convert(sysname,' '),
  13070.         GRANTEE        = convert(sysname,' '),
  13071.         TABLE_CATALOG    = convert(sysname,' '),
  13072.         TABLE_SCHEMA    = convert(sysname,' '),
  13073.         TABLE_NAME    = convert(sysname,' '),
  13074.         COLUMN_NAME    = convert(sysname,' '),
  13075.         COLUMN_GUID    = convert(binary(16),null),
  13076.         COLUMN_PROPID    = convert(int,null),
  13077.         PRIVILEGE_TYPE    = convert(varchar(30),' '),
  13078.         IS_GRANTABLE    = convert(bit,1)
  13079.     where    1=0
  13080. go
  13081. dump tran master with no_log
  13082. go
  13083.  
  13084. if (charindex('7.00', @@version) = 0 and
  13085.     charindex('8.00', @@version) = 0)
  13086. begin
  13087.     print ''
  13088.     print ''
  13089.     print 'Warning:'
  13090.     print 'you are installing the stored procedures '
  13091.     print 'on a pre 8.0 SQL Server.'
  13092.     print 'Ignore the following errors.'
  13093. end
  13094. else
  13095.     drop proc sp_column_privileges_rowset
  13096. go
  13097.  
  13098.  
  13099. /*    Procedure for 8.0 server */
  13100. CREATE PROCEDURE sp_column_privileges_rowset
  13101.     (
  13102.     @table_name         sysname,
  13103.     @table_schema        sysname = null,
  13104.     @column_name        sysname = null,
  13105.     @grantor        sysname = null,
  13106.     @grantee        sysname = null
  13107.     )
  13108. as
  13109.     select
  13110.         GRANTOR        = user_name(p.grantor),
  13111.         GRANTEE        = user_name(u.uid),
  13112.         TABLE_CATALOG    = db_name(),
  13113.         TABLE_SCHEMA    = user_name(o.uid),
  13114.         TABLE_NAME    = o.name,
  13115.         COLUMN_NAME    = c.name,
  13116.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13117.         COLUMN_PROPID    = convert(int,null),
  13118.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13119.                     case p.action
  13120.                     when 193 then N'SELECT'
  13121.                     when 195 then N'INSERT'
  13122.                     when 197 then N'UPDATE'
  13123.                     else N'REFERENCES'
  13124.                     end),
  13125.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13126.     from 
  13127.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13128.     where
  13129.         o.name = @table_name
  13130.     and     o.type in ('U','V','S')
  13131.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13132.     and    c.id = o.id
  13133.     and     (@column_name is null or @column_name = c.name)
  13134.     and     c.id = p.id
  13135.     and    (@grantor is null or @grantor = user_name(p.grantor))
  13136.     and     case 
  13137.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13138.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13139.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13140.             end & v.high <> 0            /* permission applies to this column */
  13141.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  13142.     and     v.type = 'P'
  13143.     and     v.number = c.colid
  13144.     and    (@grantee is null or @grantee = user_name(u.uid))
  13145.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13146.     and     (u.uid > 0 and u.uid < 16384)
  13147.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  13148.     and     p.protecttype <> 206    /* only grant rows */
  13149.     and     p.action in (26,193,195,197)
  13150.     and     o.uid <> u.uid            /* no rows for owner */
  13151.     and     not exists (            /* exclude revoke'd privileges */
  13152.             select *
  13153.             from sysprotects p1
  13154.             where
  13155.                 p1.protecttype = 206
  13156.             and     p1.action = p.action
  13157.             and     p1.id = p.id
  13158.             and     p1.uid = u.uid
  13159.             and     case 
  13160.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13161.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13162.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13163.                 end & v.high <> 0)            /* permission applies to this column */
  13164.     union
  13165.     select    /*    Add rows for table owner */
  13166.         GRANTOR        = user_name(u.uid),
  13167.         GRANTEE        = user_name(o.uid),
  13168.         TABLE_CATALOG    = db_name(),
  13169.         TABLE_SCHEMA    = user_name(o.uid),
  13170.         TABLE_NAME    = o.name,
  13171.         COLUMN_NAME    = c.name,
  13172.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13173.         COLUMN_PROPID    = convert(int,null),
  13174.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13175.                     case v.number
  13176.                     when 193 then N'SELECT'
  13177.                     when 195 then N'INSERT'
  13178.                     when 197 then N'UPDATE'
  13179.                     else N'REFERENCES'
  13180.                     end),
  13181.         IS_GRANTABLE    = convert(bit,1)    
  13182.     from 
  13183.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13184.     where
  13185.         o.name = @table_name
  13186.     and     o.type in ('U','V','S')
  13187.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13188.     and    (@grantee is null or @grantee = user_name(o.uid))
  13189.     and    c.id = o.id
  13190.     and     (@column_name is null or @column_name = c.name)
  13191.     and     u.uid = 1        /* grantor is dbo of database */
  13192.     and    (@grantor is null or @grantor = user_name(u.uid))
  13193.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13194.     and     v.number in (26,193,195,197)
  13195.     and     not exists (        /* exclude revoke'd privileges */
  13196.             select *
  13197.             from     sysprotects p1
  13198.             where    p1.protecttype = 206
  13199.             and     p1.action = v.number
  13200.             and     p1.id = o.id
  13201.             and     p1.uid = o.uid)
  13202.     order by 4, 5, 6, 9, 1, 2
  13203. go
  13204. dump tran master with no_log
  13205. go
  13206. CREATE PROCEDURE sp_column_privileges_rowset;2
  13207.     (
  13208.     @table_schema        sysname = null,
  13209.     @column_name        sysname = null,
  13210.     @grantor        sysname = null,
  13211.     @grantee        sysname = null
  13212.     )
  13213. as
  13214.     select
  13215.         GRANTOR        = user_name(p.grantor),
  13216.         GRANTEE        = user_name(u.uid),
  13217.         TABLE_CATALOG    = db_name(),
  13218.         TABLE_SCHEMA    = user_name(o.uid),
  13219.         TABLE_NAME    = o.name,
  13220.         COLUMN_NAME    = c.name,
  13221.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13222.         COLUMN_PROPID    = convert(int,null),
  13223.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13224.                     case p.action
  13225.                     when 193 then N'SELECT'
  13226.                     when 195 then N'INSERT'
  13227.                     when 197 then N'UPDATE'
  13228.                     else N'REFERENCES'
  13229.                     end),
  13230.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13231.     from 
  13232.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13233.     where
  13234.         o.type in ('U','V','S')
  13235.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13236.     and    c.id = o.id
  13237.     and     (@column_name is null or @column_name = c.name)
  13238.     and     c.id = p.id
  13239.     and    (@grantor is null or @grantor = user_name(p.grantor))
  13240.     and     case 
  13241.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13242.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13243.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13244.             end & v.high <> 0            /* permission applies to this column */
  13245.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  13246.     and     v.type = 'P'
  13247.     and     v.number = c.colid
  13248.     and    (@grantee is null or @grantee = user_name(u.uid))
  13249.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13250.     and     (u.uid > 0 and u.uid < 16384)
  13251.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  13252.     and     p.protecttype <> 206    /* only grant rows */
  13253.     and     p.action in (26,193,195,197)
  13254.     and     o.uid <> u.uid            /* no rows for owner */
  13255.     and     not exists (            /* exclude revoke'd privileges */
  13256.             select *
  13257.             from sysprotects p1
  13258.             where
  13259.                 p1.protecttype = 206
  13260.             and     p1.action = p.action
  13261.             and     p1.id = p.id
  13262.             and     p1.uid = u.uid
  13263.             and     case 
  13264.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13265.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13266.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13267.                 end & v.high <> 0)            /* permission applies to this column */
  13268.     union
  13269.     select    /*    Add rows for table owner */
  13270.         GRANTOR        = user_name(u.uid),
  13271.         GRANTEE        = user_name(o.uid),
  13272.         TABLE_CATALOG    = db_name(),
  13273.         TABLE_SCHEMA    = user_name(o.uid),
  13274.         TABLE_NAME    = o.name,
  13275.         COLUMN_NAME    = c.name,
  13276.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13277.         COLUMN_PROPID    = convert(int,null),
  13278.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13279.                     case v.number
  13280.                     when 193 then N'SELECT'
  13281.                     when 195 then N'INSERT'
  13282.                     when 197 then N'UPDATE'
  13283.                     else N'REFERENCES'
  13284.                     end),
  13285.         IS_GRANTABLE    = convert(bit,1)    
  13286.     from 
  13287.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13288.     where
  13289.         o.type in ('U','V','S')
  13290.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13291.     and    (@grantee is null or @grantee = user_name(o.uid))
  13292.     and    c.id = o.id
  13293.     and     (@column_name is null or @column_name = c.name)
  13294.     and     u.uid = 1        /* grantor is dbo of database */
  13295.     and    (@grantor is null or @grantor = user_name(u.uid))
  13296.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13297.     and     v.number in (26,193,195,197)
  13298.     and     not exists (        /* exclude revoke'd privileges */
  13299.             select *
  13300.             from     sysprotects p1
  13301.             where    p1.protecttype = 206
  13302.             and     p1.action = v.number
  13303.             and     p1.id = o.id
  13304.             and     p1.uid = o.uid)
  13305.     order by 4, 5, 6, 9, 1, 2
  13306. go
  13307. dump tran master with no_log
  13308. go
  13309. create procedure sp_column_privileges_rowset;5
  13310.     (
  13311.     @table_server        sysname,
  13312.     @table_catalog        sysname = null,
  13313.     @table_name        sysname = null,
  13314.     @table_schema        sysname = null,
  13315.     @column_name        sysname = null,
  13316.     @grantor        sysname = null,
  13317.     @grantee        sysname = null
  13318.     )
  13319. as
  13320.     select
  13321.         GRANTOR,
  13322.         GRANTEE,
  13323.         TABLE_CATALOG,
  13324.         TABLE_SCHEMA,
  13325.         TABLE_NAME,
  13326.         COLUMN_NAME,
  13327.         COLUMN_GUID,
  13328.         COLUMN_PROPID,    
  13329.         PRIVILEGE_TYPE,
  13330.         IS_GRANTABLE    
  13331.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  13332.                 @table_server,
  13333.                 @table_catalog,
  13334.                 @table_schema,
  13335.                 @table_name,
  13336.                 @column_name,
  13337.                 @grantor,
  13338.                 @grantee>
  13339.     order by 3, 4, 5, 6, 9, 1, 2
  13340. go
  13341.  
  13342. grant execute on sp_column_privileges_rowset to public
  13343. go
  13344.  
  13345. dump tran master with no_log
  13346. go
  13347. if (charindex('6.00', @@version) > 0)
  13348.     begin
  13349.     if (exists (select * from sysobjects
  13350.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  13351.         begin
  13352.         drop procedure sp_column_privileges_rowset
  13353.         dump tran master with no_log
  13354.         end
  13355.     end
  13356. go
  13357.  
  13358.  
  13359. print ''
  13360. print 'creating sp_columns_rowset'
  13361. go
  13362.  
  13363. /*    Procedure for 6.0 and 6.50 server */
  13364. create procedure sp_columns_rowset
  13365.     (
  13366.        @table_name    varchar(255),
  13367.     @table_schema     varchar(255) = null,
  13368.     @column_name    varchar(255) = null
  13369.     )
  13370. as     
  13371.     select
  13372.         TABLE_CATALOG        = db_name(),
  13373.         TABLE_SCHEMA        = user_name(o.uid),
  13374.         TABLE_NAME        = o.name,
  13375.         COLUMN_NAME        = c.name,
  13376.         COLUMN_GUID        = convert(binary(16),null),
  13377.         COLUMN_PROPID        = convert(int,null),
  13378.         ORDINAL_POSITION     = convert(int,
  13379.                        (
  13380.                         select count(*)
  13381.                         from syscolumns sc
  13382.                         where sc.id     =  c.id
  13383.                           AND sc.number =  c.number
  13384.                           AND sc.colid  <= c.colid
  13385.                         )),
  13386.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13387.  
  13388.         COLUMN_DEFAULT        = convert (varchar (255),
  13389.                                   case when substring(m.text,1,1) = '('
  13390.                                   then substring(m.text,2,datalength(m.text)-2)
  13391.                                   else substring(m.text,1,datalength(m.text)-2)
  13392.                               end), 
  13393.         COLUMN_FLAGS        = convert(int,
  13394.                         case when d.is_long = 1 
  13395.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13396.                         else 0
  13397.                         end
  13398.                     |    case when d.fixlen is not null
  13399.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13400.                         else 0
  13401.                         end
  13402.                     |    case when c.status&8 = 8
  13403.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13404.                         else 0
  13405.                         end
  13406.                     |    case 
  13407.                         when d.type_name = 'timestamp'
  13408.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13409.                         when (c.status&128) != 128
  13410.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13411.                         else 0 
  13412.                         end),
  13413.         IS_NULLABLE        = convert(bit,c.status&8),
  13414.         DATA_TYPE        = d.oledb_data_type,
  13415.         TYPE_GUID        = convert(binary(16),null),
  13416.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13417.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13418.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13419.                         then coalesce(d.column_size,c.length)
  13420.                         else null 
  13421.                         end),
  13422.         CHARACTER_OCTET_LENGTH    = convert(int,
  13423.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13424.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13425.                         then coalesce(d.column_size,c.length)
  13426.                         else null 
  13427.                         end),
  13428.         NUMERIC_PRECISION    = convert(smallint,
  13429.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13430.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13431.                                     then d.data_precision else null end),
  13432.         NUMERIC_SCALE        = convert(smallint, 
  13433.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13434.         DATETIME_PRECISION    = convert(int,
  13435.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13436.                             when data_precision = 23 then 3 else 0 end),
  13437.         CHARACTER_SET_CATALOG    = convert(sysname,
  13438.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13439.                         then 'master' 
  13440.                         else null 
  13441.                         end),
  13442.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13443.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13444.                         then 'dbo' 
  13445.                         else null 
  13446.                         end),
  13447.         CHARACTER_SET_NAME    = convert(sysname,
  13448.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13449.                         then a_cha.name 
  13450.                         else null 
  13451.                         end),
  13452.         COLLATION_CATALOG    = convert(sysname,
  13453.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13454.                         then 'master' 
  13455.                         else null 
  13456.                         end),
  13457.         COLLATION_SCHEMA    = convert(sysname,
  13458.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13459.                         then 'dbo' 
  13460.                         else null 
  13461.                         end),
  13462.         COLLATION_NAME        = convert(sysname,
  13463.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13464.                         then b_cha.name 
  13465.                         else null 
  13466.                         end),
  13467.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13468.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13469.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13470.         DESCRIPTION        = convert(varchar(1),null)    
  13471.     
  13472.     from
  13473.         syscolumns c,
  13474.         syscomments m,
  13475.         sysobjects o,
  13476.         master.dbo.spt_provider_types d,
  13477.         systypes t,
  13478.         sysusers u,
  13479.         master.dbo.sysconfigures    cfg,
  13480.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  13481.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  13482.     where
  13483.         o.name = @table_name
  13484.     and     o.type in ('U','V','S')
  13485.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13486.     and     (
  13487.         suser_id() = 1     /* User is the System Administrator */
  13488.         or o.uid = user_id()     /* User created the object */
  13489.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13490.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13491.             from sysprotects p
  13492.             where p.id = o.id
  13493.             /* get rows for public,current user,user's group */
  13494.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13495.             /* check for SELECT,EXECUTE privilege */
  13496.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13497.             ) = 1    /* final magic...compare Grants      */
  13498.            )
  13499.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13500.     and    (@column_name is null or @column_name = c.name)
  13501.     and     o.id = c.id
  13502.     and     t.type = d.ss_dtype
  13503.     and    (t.usertype != 80 or d.type_name='timestamp')
  13504.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13505.     and     c.usertype = t.usertype
  13506.     and    c.cdefault *= m.id
  13507.     and    m.colid = 1
  13508.     and    cfg.comment = 'default sortorder ID' 
  13509.     and    a_cha.type = 1001 /* type is charset */
  13510.     and    b_cha.type = 2001 /* type is sortorder */
  13511.     and    a_cha.id = b_cha.csid
  13512.     and     b_cha.id = cfg.value
  13513.     order by 2, 3, c.colid
  13514. go
  13515.  
  13516. dump tran master with no_log
  13517. go
  13518.  
  13519. create procedure sp_columns_rowset;2
  13520.     (
  13521.     @table_schema     varchar(255) = null,
  13522.     @column_name    varchar(255) = null
  13523.     )
  13524. as     
  13525.     select
  13526.         TABLE_CATALOG        = db_name(),
  13527.         TABLE_SCHEMA        = user_name(o.uid),
  13528.         TABLE_NAME        = o.name,
  13529.         COLUMN_NAME        = c.name,
  13530.         COLUMN_GUID        = convert(binary(16),null),
  13531.         COLUMN_PROPID        = convert(int,null),
  13532.         ORDINAL_POSITION     = convert(int,
  13533.                        (
  13534.                         select count(*)
  13535.                         from syscolumns sc
  13536.                         where sc.id     =  c.id
  13537.                           AND sc.number =  c.number
  13538.                           AND sc.colid  <= c.colid
  13539.                         )),
  13540.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13541.         COLUMN_DEFAULT        = convert (varchar (255),
  13542.                                   case when substring(m.text,1,1) = '('
  13543.                                   then substring(m.text,2,datalength(m.text)-2)
  13544.                                   else substring(m.text,1,datalength(m.text)-2)
  13545.                               end), 
  13546.         COLUMN_FLAGS        = convert(int,
  13547.                         case when d.is_long = 1 
  13548.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13549.                         else 0
  13550.                         end
  13551.                     |    case when d.fixlen is not null
  13552.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13553.                         else 0
  13554.                         end
  13555.                     |    case when c.status&8 = 8
  13556.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13557.                         else 0
  13558.                         end
  13559.                     |    case 
  13560.                         when d.type_name = 'timestamp'
  13561.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13562.                         when (c.status&128) != 128
  13563.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13564.                         else 0 
  13565.                         end),
  13566.         IS_NULLABLE        = convert(bit,c.status&8),
  13567.         DATA_TYPE        = d.oledb_data_type,
  13568.         TYPE_GUID        = convert(binary(16),null),
  13569.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13570.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13571.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13572.                         then coalesce(d.column_size,c.length)
  13573.                         else null 
  13574.                         end),
  13575.         CHARACTER_OCTET_LENGTH    = convert(int,
  13576.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13577.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13578.                         then coalesce(d.column_size,c.length)
  13579.                         else null 
  13580.                         end),
  13581.         NUMERIC_PRECISION    = convert(smallint,
  13582.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13583.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13584.                             then d.data_precision else null end),
  13585.         NUMERIC_SCALE        = convert(smallint, 
  13586.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13587.         DATETIME_PRECISION    = convert(int,
  13588.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13589.                             when data_precision = 23 then 3 else 0 end),
  13590.         CHARACTER_SET_CATALOG    = convert(sysname,
  13591.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13592.                         then 'master' 
  13593.                         else null 
  13594.                         end),
  13595.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13596.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13597.                         then 'dbo' 
  13598.                         else null 
  13599.                         end),
  13600.         CHARACTER_SET_NAME    = convert(sysname,
  13601.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13602.                         then a_cha.name 
  13603.                         else null 
  13604.                         end),
  13605.         COLLATION_CATALOG    = convert(sysname,
  13606.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13607.                         then 'master' 
  13608.                         else null 
  13609.                         end),
  13610.         COLLATION_SCHEMA    = convert(sysname,
  13611.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13612.                         then 'dbo' 
  13613.                         else null 
  13614.                         end),
  13615.         COLLATION_NAME        = convert(sysname,
  13616.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13617.                         then b_cha.name 
  13618.                         else null 
  13619.                         end),
  13620.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13621.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13622.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13623.         DESCRIPTION        = convert(varchar(1),null)    
  13624.     
  13625.     from
  13626.         syscolumns c,
  13627.         syscomments m,
  13628.         sysobjects o,
  13629.         master.dbo.spt_provider_types d,
  13630.         systypes t,
  13631.         sysusers u,
  13632.         master.dbo.sysconfigures    cfg,
  13633.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  13634.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  13635.     where
  13636.          o.type in ('U','V','S')
  13637.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13638.     and     (
  13639.         suser_id() = 1     /* User is the System Administrator */
  13640.         or o.uid = user_id()     /* User created the object */
  13641.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13642.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13643.             from sysprotects p
  13644.             where p.id = o.id
  13645.             /* get rows for public,current user,user's group */
  13646.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13647.             /* check for SELECT,EXECUTE privilege */
  13648.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13649.             ) = 1    /* final magic...compare Grants      */
  13650.            )
  13651.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13652.     and    (@column_name is null or @column_name = c.name)
  13653.     and     o.id = c.id
  13654.     and     t.type = d.ss_dtype
  13655.     and    (t.usertype != 80 or d.type_name='timestamp')
  13656.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13657.     and     c.usertype = t.usertype
  13658.     and    c.cdefault *= m.id
  13659.     and     m.colid = 1
  13660.     and    cfg.comment = 'default sortorder ID' 
  13661.     and    a_cha.type = 1001 /* type is charset */
  13662.     and    b_cha.type = 2001 /* type is sortorder */
  13663.     and    a_cha.id = b_cha.csid
  13664.     and     b_cha.id = cfg.value
  13665.     order by 2, 3, c.colid
  13666.     
  13667. go
  13668.  
  13669. dump tran master with no_log
  13670. go
  13671.  
  13672. if (charindex('7.00', @@version) = 0 and
  13673.     charindex('8.00', @@version) = 0)
  13674. begin
  13675.     print ''
  13676.     print ''
  13677.     print 'Warning:'
  13678.     print 'you are installing the stored procedures '
  13679.     print 'on a pre 8.0 SQL Server.'
  13680.     print 'Ignore the following errors.'
  13681. end
  13682. else
  13683.     drop proc sp_columns_rowset
  13684. go
  13685.  
  13686. /*    Procedure for 7.0 server */
  13687. create procedure sp_columns_rowset
  13688.     (
  13689.        @table_name    sysname,
  13690.     @table_schema     sysname = NULL,
  13691.     @column_name    sysname = NULL
  13692.     )
  13693. as
  13694.     select
  13695.         TABLE_CATALOG        = db_name(),
  13696.         TABLE_SCHEMA        = user_name(o.uid),
  13697.         TABLE_NAME        = o.name,
  13698.         COLUMN_NAME        = c.name,
  13699.         COLUMN_GUID        = convert(uniqueidentifier,null),
  13700.         COLUMN_PROPID        = convert(int,null),
  13701.         ORDINAL_POSITION     = convert(int,
  13702.                        (
  13703.                         select count(*)
  13704.                         from syscolumns sc
  13705.                         where sc.id     =  c.id
  13706.                           AND sc.number =  c.number
  13707.                           AND sc.colid  <= c.colid
  13708.                         )),
  13709.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13710.         COLUMN_DEFAULT        =  convert(nvarchar(2000),
  13711.                                         case when substring(m.text,1,1) = '('
  13712.                                             then substring(m.text,2,datalength(m.text)/2-2)
  13713.                                             else substring(m.text,1,datalength(m.text)/2-2)
  13714.                                         end    ), 
  13715.         COLUMN_FLAGS        = convert(int,
  13716.                         case when d.is_long = 1 
  13717.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13718.                         else 0
  13719.                         end
  13720.                     |    case when d.fixlen is not null
  13721.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13722.                         else 0
  13723.                         end
  13724.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  13725.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13726.                         else 0
  13727.                         end
  13728.                     |    case 
  13729.                         when d.type_name = 'timestamp'
  13730.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13731.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  13732.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  13733.                         else 0 
  13734.                         end),
  13735.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  13736.         DATA_TYPE        = d.oledb_data_type,
  13737.         TYPE_GUID        = convert(uniqueidentifier,null),
  13738.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13739.                         case 
  13740.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13741.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13742.                         then coalesce(d.column_size,c.length)
  13743.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13744.                         then coalesce(d.column_size,c.length/2)
  13745.                         else null 
  13746.                         end),
  13747.         CHARACTER_OCTET_LENGTH    = convert(int,
  13748.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13749.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13750.                         then coalesce(d.column_size,c.length)
  13751.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13752.                         then coalesce(d.column_size*2,c.length)
  13753.                         else null 
  13754.                         end),
  13755.         NUMERIC_PRECISION    = convert(smallint,
  13756.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  13757.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13758.                             then d.data_precision else null end),
  13759.         NUMERIC_SCALE        = convert(smallint, 
  13760.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13761.         DATETIME_PRECISION    = convert(int,
  13762.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13763.                             when data_precision = 23 then 3 else 0 end),
  13764.         CHARACTER_SET_CATALOG    = convert(sysname,
  13765.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13766.                         then N'master' 
  13767.                         else null 
  13768.                         end),
  13769.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13770.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13771.                         then N'dbo' 
  13772.                         else null 
  13773.                         end),
  13774.         CHARACTER_SET_NAME    = convert(sysname,
  13775.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13776.                         then a_cha.name 
  13777.                         else null 
  13778.                         end),
  13779.         COLLATION_CATALOG    = convert(sysname,
  13780.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13781.                         then N'master' 
  13782.                         else null 
  13783.                         end),
  13784.         COLLATION_SCHEMA    = convert(sysname,
  13785.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13786.                         then N'dbo' 
  13787.                         else null 
  13788.                         end),
  13789.         COLLATION_NAME        = convert(sysname,
  13790.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13791.                         then b_cha.name 
  13792.                         else null 
  13793.                         end),
  13794.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13795.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13796.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13797.         DESCRIPTION        = convert(nvarchar(1),null)    
  13798.  
  13799.     from
  13800.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  13801.         sysobjects o,
  13802.         master.dbo.spt_provider_types d,
  13803.         systypes t,
  13804.         master.dbo.sysconfigures    cfg,
  13805.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  13806.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  13807.     where
  13808.         permissions(o.id, c.name) <> 0
  13809.     and    o.name = @table_name
  13810.     and     o.type in ('U','V','S')
  13811.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13812.     and    (@column_name is null or @column_name = c.name)
  13813.     and     o.id = c.id
  13814.     and     t.xtype = d.ss_dtype
  13815.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13816.     and     c.xusertype = t.xusertype
  13817.     and    cfg.comment = 'default sortorder id' 
  13818.     and    a_cha.type = 1001 /* type is charset */
  13819.     and    b_cha.type = 2001 /* type is sortorder */
  13820.     and    a_cha.id = b_cha.csid
  13821.     and     b_cha.id = cfg.value
  13822.     order by 2, 3, c.colorder
  13823. go
  13824.  
  13825. dump tran master with no_log
  13826. go
  13827.  
  13828. /*    Procedure for 7.0 server */
  13829. create procedure sp_columns_rowset;2
  13830.     (
  13831.     @table_schema     sysname = NULL,
  13832.     @column_name    sysname = NULL
  13833.     )
  13834. as     
  13835.     select
  13836.         TABLE_CATALOG        = db_name(),
  13837.         TABLE_SCHEMA        = user_name(o.uid),
  13838.         TABLE_NAME        = o.name,
  13839.         COLUMN_NAME        = c.name,
  13840.         COLUMN_GUID        = convert(uniqueidentifier,null),
  13841.         COLUMN_PROPID        = convert(int,null),
  13842.         ORDINAL_POSITION     = convert(int,
  13843.                        (
  13844.                         select count(*)
  13845.                         from syscolumns sc
  13846.                         where sc.id     =  c.id
  13847.                           AND sc.number =  c.number
  13848.                           AND sc.colid  <= c.colid
  13849.                         )),
  13850.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13851.         COLUMN_DEFAULT        =  convert(nvarchar(2000),
  13852.                                         case when substring(m.text,1,1) = '('
  13853.                                             then substring(m.text,2,datalength(m.text)/2-2)
  13854.                                             else substring(m.text,1,datalength(m.text)/2-2)
  13855.                                         end    ), 
  13856.         COLUMN_FLAGS        = convert(int,
  13857.                         case when d.is_long = 1 
  13858.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13859.                         else 0
  13860.                         end
  13861.                     |    case when d.fixlen is not null
  13862.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13863.                         else 0
  13864.                         end
  13865.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  13866.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13867.                         else 0
  13868.                         end
  13869.                     |    case 
  13870.                         when d.type_name = 'timestamp'
  13871.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13872.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  13873.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  13874.                         else 0 
  13875.                         end),
  13876.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  13877.         DATA_TYPE        = d.oledb_data_type,
  13878.         TYPE_GUID        = convert(uniqueidentifier,null),
  13879.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13880.                         case 
  13881.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13882.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13883.                         then coalesce(d.column_size,c.length)
  13884.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13885.                         then coalesce(d.column_size,c.length/2)
  13886.                         else null 
  13887.                         end),
  13888.         CHARACTER_OCTET_LENGTH    = convert(int,
  13889.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13890.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13891.                         then coalesce(d.column_size,c.length)
  13892.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13893.                         then coalesce(d.column_size*2,c.length)
  13894.                         else null 
  13895.                         end),
  13896.         NUMERIC_PRECISION    = convert(smallint,
  13897.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  13898.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13899.                             then d.data_precision else null end),
  13900.         NUMERIC_SCALE        = convert(smallint, 
  13901.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13902.         DATETIME_PRECISION    = convert(int,
  13903.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13904.                             when data_precision = 23 then 3 else 0 end),
  13905.         CHARACTER_SET_CATALOG    = convert(sysname,
  13906.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13907.                         then N'master' 
  13908.                         else null 
  13909.                         end),
  13910.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13911.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13912.                         then N'dbo' 
  13913.                         else null 
  13914.                         end),
  13915.         CHARACTER_SET_NAME    = convert(sysname,
  13916.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13917.                         then a_cha.name 
  13918.                         else null 
  13919.                         end),
  13920.         COLLATION_CATALOG    = convert(sysname,
  13921.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13922.                         then N'master' 
  13923.                         else null 
  13924.                         end),
  13925.         COLLATION_SCHEMA    = convert(sysname,
  13926.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13927.                         then N'dbo' 
  13928.                         else null 
  13929.                         end),
  13930.         COLLATION_NAME        = convert(sysname,
  13931.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13932.                         then b_cha.name 
  13933.                         else null 
  13934.                         end),
  13935.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13936.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13937.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13938.         DESCRIPTION        = convert(nvarchar(1),null)    
  13939.     
  13940.     from
  13941.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  13942.         sysobjects o,
  13943.         master.dbo.spt_provider_types d,
  13944.         systypes t,
  13945.         master.dbo.sysconfigures    cfg,
  13946.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder.*/
  13947.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset.*/
  13948.     where
  13949.         permissions(o.id, c.name) <> 0
  13950.     and     o.type in ('U','V','S')
  13951.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13952.     and    (@column_name is null or @column_name = c.name)
  13953.     and     o.id = c.id
  13954.     and     t.xtype = d.ss_dtype
  13955.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13956.     and     c.xusertype = t.xusertype
  13957.     and    cfg.comment = 'default sortorder id' 
  13958.     and     a_cha.type = 1001 /* type is charset */
  13959.     and     b_cha.type = 2001 /* type is sortorder */
  13960.     and    a_cha.id = b_cha.csid
  13961.     and     b_cha.id = cfg.value
  13962.     order by 2, 3, c.colorder
  13963. go
  13964.  
  13965. dump tran master with no_log
  13966. go
  13967.  
  13968. /*    Procedure for 7.0 server */
  13969. create procedure sp_columns_rowset;5
  13970.     (
  13971.     @table_server        sysname,
  13972.     @table_catalog        sysname = null,
  13973.     @table_name        sysname = null,
  13974.     @table_schema        sysname = null,
  13975.     @column_name        sysname = null
  13976.     )
  13977. as
  13978.     select
  13979.         TABLE_CATALOG,
  13980.         TABLE_SCHEMA,
  13981.         TABLE_NAME,
  13982.         COLUMN_NAME,
  13983.         COLUMN_GUID,
  13984.         COLUMN_PROPID,
  13985.         ORDINAL_POSITION,
  13986.         COLUMN_HASDEFAULT,
  13987.         COLUMN_DEFAULT,
  13988.         COLUMN_FLAGS,
  13989.         IS_NULLABLE,
  13990.         DATA_TYPE,
  13991.         TYPE_GUID,
  13992.         CHARACTER_MAXIMUM_LENGTH,
  13993.         CHARACTER_OCTET_LENGTH,
  13994.         NUMERIC_PRECISION,
  13995.         NUMERIC_SCALE,
  13996.         DATETIME_PRECISION,
  13997.         CHARACTER_SET_CATALOG,
  13998.         CHARACTER_SET_SCHEMA,
  13999.         CHARACTER_SET_NAME,
  14000.         COLLATION_CATALOG,
  14001.         COLLATION_SCHEMA,
  14002.         COLLATION_NAME,
  14003.         DOMAIN_CATALOG,
  14004.         DOMAIN_SCHEMA,
  14005.         DOMAIN_NAME,
  14006.         DESCRIPTION
  14007.     from master.dbo.SYSREMOTE_COLUMNS <
  14008.                 @table_server,
  14009.                 @table_catalog,
  14010.                 @table_schema,
  14011.                 @table_name,
  14012.                 @column_name > 
  14013.     order by 1, 2, 3, 7
  14014. go
  14015.  
  14016.  
  14017. dump tran master with no_log
  14018. go
  14019.  
  14020. /*    Procedure for 8.00 server */
  14021. if (charindex('8.00', @@version) = 0)
  14022. begin
  14023.     print ''
  14024.     print ''
  14025.     print 'Warning:'
  14026.     print 'you are installing the stored procedures '
  14027.     print 'on a pre 8.0 SQL Server.'
  14028.     print 'Ignore the following errors.'
  14029. end
  14030. else
  14031.     drop proc sp_columns_rowset
  14032. go
  14033.  
  14034. /*    Procedure for 8.00 server */
  14035. create procedure sp_columns_rowset
  14036.     (
  14037.        @table_name    sysname,
  14038.     @table_schema     sysname = NULL,
  14039.     @column_name    sysname = NULL
  14040.     )
  14041. as     
  14042.     select
  14043.         TABLE_CATALOG        = db_name(),
  14044.         TABLE_SCHEMA        = user_name(o.uid),
  14045.         TABLE_NAME        = o.name,
  14046.         COLUMN_NAME        = c.name,
  14047.         COLUMN_GUID        = convert(uniqueidentifier,null),
  14048.         COLUMN_PROPID        = convert(int,null),
  14049.         ORDINAL_POSITION     = convert(int,
  14050.                        (
  14051.                         select count(*)
  14052.                         from syscolumns sc
  14053.                         where sc.id     =  c.id
  14054.                           AND sc.number =  c.number
  14055.                           AND sc.colid  <= c.colid
  14056.                         )),
  14057.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  14058.  
  14059.         /* Get rid of ( if it is first char otherwise display the whole thing */
  14060.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  14061.                                         case when substring(m.text,1,1) = '('
  14062.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14063.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14064.                                         end    ), 
  14065.  
  14066.         COLUMN_FLAGS        = convert(int,
  14067.                         case when d.is_long = 1 
  14068.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14069.                         else 0
  14070.                         end
  14071.                     |    case when d.fixlen is not null
  14072.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14073.                         else 0
  14074.                         end
  14075.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14076.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14077.                         else 0
  14078.                         end
  14079.                     |    case 
  14080.                         when d.type_name = 'timestamp'
  14081.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14082.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  14083.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14084.                         else 0 
  14085.                         end),
  14086.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14087.         DATA_TYPE        = d.oledb_data_type,
  14088.         TYPE_GUID        = convert(uniqueidentifier,null),
  14089.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14090.                         case 
  14091.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14092.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14093.                         then coalesce(d.column_size,c.length)
  14094.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14095.                         then coalesce(d.column_size,c.length/2)
  14096.                         else null 
  14097.                         end),
  14098.         CHARACTER_OCTET_LENGTH    = convert(int,
  14099.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14100.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14101.                         then coalesce(d.column_size,c.length)
  14102.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14103.                         then coalesce(d.column_size*2,c.length)
  14104.                         else null 
  14105.                         end),
  14106.         NUMERIC_PRECISION    = convert(smallint,
  14107.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14108.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14109.                             then d.data_precision else null end),
  14110.         NUMERIC_SCALE        = convert(smallint, 
  14111.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14112.         DATETIME_PRECISION    = convert(int,
  14113.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14114.                             when data_precision = 23 then 3 else 0 end),
  14115.         CHARACTER_SET_CATALOG    = convert(sysname,
  14116.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14117.                         then N'master' 
  14118.                         else null 
  14119.                         end),
  14120.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14121.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14122.                         then N'dbo' 
  14123.                         else null 
  14124.                         end),
  14125.         CHARACTER_SET_NAME    = convert(sysname,
  14126.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14127.                         then a_cha.name 
  14128.                         else null 
  14129.                         end),
  14130.         COLLATION_CATALOG    = convert(sysname,
  14131.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14132.                         then N'master' 
  14133.                         else null 
  14134.                         end),
  14135.         COLLATION_SCHEMA    = convert(sysname,
  14136.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14137.                         then N'dbo' 
  14138.                         else null 
  14139.                         end),
  14140.         COLLATION_NAME        = convert(sysname,
  14141.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14142.                         then c.collation 
  14143.                         else null 
  14144.                         end),
  14145.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  14146.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  14147.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  14148.         DESCRIPTION        = convert(nvarchar(1),null),
  14149.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14150.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14151.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14152.                                when ABS(c.collationid) > 0x1000000 
  14153.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14154.                             else null 
  14155.                         end,
  14156.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14157.         IS_COMPUTED    = convert(bit, c.iscomputed)    
  14158.     from
  14159.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14160.         sysobjects o,
  14161.         master.dbo.spt_provider_types d,
  14162.         systypes t,
  14163.         master.dbo.syscharsets        a_cha /* charset/1001, not sortorder. */
  14164.     where
  14165.         permissions(o.id, c.name) <> 0
  14166.     and    o.name = @table_name
  14167.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14168.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  14169.     and    (@column_name is null or @column_name = c.name)
  14170.     and     o.id = c.id
  14171.     and     t.xtype = d.ss_dtype
  14172.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14173.     and     c.xusertype = t.xusertype
  14174.     and    a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14175.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14176.     order by 2, 3, c.colorder
  14177. go
  14178.  
  14179.  
  14180. /*    Procedure for 8.0 server */
  14181. create procedure sp_columns_rowset;2
  14182.     (
  14183.     @table_schema     sysname = NULL,
  14184.     @column_name    sysname = NULL
  14185.     )
  14186. as     
  14187.     select
  14188.         TABLE_CATALOG        = db_name(),
  14189.         TABLE_SCHEMA        = user_name(o.uid),
  14190.         TABLE_NAME        = o.name,
  14191.         COLUMN_NAME        = c.name,
  14192.         COLUMN_GUID        = convert(uniqueidentifier,null),
  14193.         COLUMN_PROPID        = convert(int,null),
  14194.         ORDINAL_POSITION     = convert(int,
  14195.                        (
  14196.                         select count(*)
  14197.                         from syscolumns sc
  14198.                         where sc.id     =  c.id
  14199.                           AND sc.number =  c.number
  14200.                           AND sc.colid  <= c.colid
  14201.                         )),
  14202.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  14203.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  14204.                                         case when substring(m.text,1,1) = '('
  14205.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14206.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14207.                                         end    ), 
  14208.         COLUMN_FLAGS        = convert(int,
  14209.                         case when d.is_long = 1 
  14210.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14211.                         else 0
  14212.                         end
  14213.                     |    case when d.fixlen is not null
  14214.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14215.                         else 0
  14216.                         end
  14217.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14218.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14219.                         else 0
  14220.                         end
  14221.                     |    case 
  14222.                         when d.type_name = 'timestamp'
  14223.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14224.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  14225.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14226.                         else 0 
  14227.                         end),
  14228.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14229.         DATA_TYPE        = d.oledb_data_type,
  14230.         TYPE_GUID        = convert(uniqueidentifier,null),
  14231.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14232.                         case 
  14233.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14234.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14235.                         then coalesce(d.column_size,c.length)
  14236.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14237.                         then coalesce(d.column_size,c.length/2)
  14238.                         else null 
  14239.                         end),
  14240.         CHARACTER_OCTET_LENGTH    = convert(int,
  14241.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14242.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14243.                         then coalesce(d.column_size,c.length)
  14244.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14245.                         then coalesce(d.column_size*2,c.length)
  14246.                         else null 
  14247.                         end),
  14248.         NUMERIC_PRECISION    = convert(smallint,
  14249.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14250.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14251.                             then d.data_precision else null end),
  14252.         NUMERIC_SCALE        = convert(smallint, 
  14253.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14254.         DATETIME_PRECISION    = convert(int,
  14255.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14256.                             when data_precision = 23 then 3 else 0 end),
  14257.         CHARACTER_SET_CATALOG    = convert(sysname,
  14258.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14259.                         then N'master' 
  14260.                         else null 
  14261.                         end),
  14262.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14263.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14264.                         then N'dbo' 
  14265.                         else null 
  14266.                         end),
  14267.         CHARACTER_SET_NAME    = convert(sysname,
  14268.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14269.                         then a_cha.name 
  14270.                         else null 
  14271.                         end),
  14272.         COLLATION_CATALOG    = convert(sysname,
  14273.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14274.                         then N'master' 
  14275.                         else null 
  14276.                         end),
  14277.         COLLATION_SCHEMA    = convert(sysname,
  14278.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14279.                         then N'dbo' 
  14280.                         else null 
  14281.                         end),
  14282.         COLLATION_NAME        = convert(sysname,
  14283.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14284.                         then c.collation 
  14285.                         else null 
  14286.                         end),
  14287.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  14288.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  14289.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  14290.         DESCRIPTION        = convert(nvarchar(1),null),    
  14291.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14292.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14293.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14294.                                when ABS(c.collationid) > 0x1000000 
  14295.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14296.                             else null 
  14297.                         end,
  14298.         
  14299.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14300.         IS_COMPUTED = convert(bit, c.iscomputed)
  14301.     
  14302.     from
  14303.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14304.         sysobjects o,
  14305.         master.dbo.spt_provider_types d,
  14306.         systypes t,
  14307.         master.dbo.syscharsets        a_cha /* charset/1001, not sortorder.*/
  14308.     where
  14309.         permissions(o.id, c.name) <> 0
  14310.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14311.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  14312.     and    (@column_name is null or @column_name = c.name)
  14313.     and     o.id = c.id
  14314.     and     t.xtype = d.ss_dtype
  14315.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14316.     and     c.xusertype = t.xusertype
  14317.     and    a_cha.type = 1001 /* type is charset */
  14318.     and    a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14319.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14320.     order by 2, 3, c.colorder
  14321. go
  14322.  
  14323. dump tran master with no_log
  14324. go
  14325.  
  14326. /*    Procedure for 8.0 server */
  14327. create procedure sp_columns_rowset;5
  14328.     (
  14329.     @table_server        sysname,
  14330.     @table_catalog        sysname = null,
  14331.     @table_name        sysname = null,
  14332.     @table_schema        sysname = null,
  14333.     @column_name        sysname = null
  14334.     )
  14335. as
  14336.     select
  14337.         TABLE_CATALOG,
  14338.         TABLE_SCHEMA,
  14339.         TABLE_NAME,
  14340.         COLUMN_NAME,
  14341.         COLUMN_GUID,
  14342.         COLUMN_PROPID,
  14343.         ORDINAL_POSITION,
  14344.         COLUMN_HASDEFAULT,
  14345.         COLUMN_DEFAULT,
  14346.         COLUMN_FLAGS,
  14347.         IS_NULLABLE,
  14348.         DATA_TYPE,
  14349.         TYPE_GUID,
  14350.         CHARACTER_MAXIMUM_LENGTH,
  14351.         CHARACTER_OCTET_LENGTH,
  14352.         NUMERIC_PRECISION,
  14353.         NUMERIC_SCALE,
  14354.         DATETIME_PRECISION,
  14355.         CHARACTER_SET_CATALOG,
  14356.         CHARACTER_SET_SCHEMA,
  14357.         CHARACTER_SET_NAME,
  14358.         COLLATION_CATALOG,
  14359.         COLLATION_SCHEMA,
  14360.         COLLATION_NAME,
  14361.         DOMAIN_CATALOG,
  14362.         DOMAIN_SCHEMA,
  14363.         DOMAIN_NAME,
  14364.         DESCRIPTION
  14365.  
  14366.     from master.dbo.SYSREMOTE_COLUMNS <
  14367.                 @table_server,
  14368.                 @table_catalog,
  14369.                 @table_schema,
  14370.                 @table_name,
  14371.                 @column_name > 
  14372.     order by 1, 2, 3, 7
  14373. go
  14374.  
  14375.  
  14376. grant execute on sp_columns_rowset to public
  14377. go
  14378.  
  14379. dump tran master with no_log
  14380. go
  14381. if (charindex('6.00', @@version) > 0)
  14382.     begin
  14383.     if (exists (select * from sysobjects
  14384.             where name = 'sp_columns_rowset' and type = 'P '))
  14385.         begin
  14386.         drop procedure sp_columns_rowset
  14387.         dump tran master with no_log
  14388.         end
  14389.     end
  14390. go
  14391.  
  14392. print ''
  14393. print 'creating sp_check_constraints_rowset'
  14394. go
  14395.  
  14396. /*    Procedure for 6.50 and earlier servers */
  14397. create procedure sp_check_constraints_rowset
  14398.     (
  14399.     @constraint_name    varchar(255),
  14400.     @constraint_schema    varchar(255) = null
  14401.     )
  14402. as
  14403.     select
  14404.         CONSTRAINT_CATALOG    = db_name(),
  14405.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14406.         CONSTRAINT_NAME        = c_obj.name,
  14407.         CHECK_CLAUSE        = m.text,    
  14408.         DESCRIPTION            = convert(varchar(1),null)
  14409.     from 
  14410.         sysobjects c_obj, syscomments m
  14411.     where
  14412.             c_obj.type = 'C '
  14413.         and c_obj.name    = @constraint_name
  14414.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14415.         and c_obj.id = m.id
  14416.     order by 1,2,3 
  14417. go
  14418. dump tran master with no_log
  14419. go
  14420.  
  14421. create procedure sp_check_constraints_rowset;2
  14422.     (
  14423.     @constraint_schema    varchar(255) = null
  14424.     )
  14425. as
  14426.     select
  14427.         CONSTRAINT_CATALOG    = db_name(),
  14428.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14429.         CONSTRAINT_NAME        = c_obj.name,
  14430.         CHECK_CLAUSE        = m.text,    
  14431.         DESCRIPTION            = convert(varchar(1),null)
  14432.     from 
  14433.         sysobjects c_obj, syscomments m
  14434.     where
  14435.             c_obj.type = 'C '
  14436.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14437.         and c_obj.id = m.id
  14438.     order by 1,2,3 
  14439. go
  14440. dump tran master with no_log
  14441. go
  14442.  
  14443. if (charindex('7.00', @@version) > 0 or
  14444.     charindex('8.00', @@version) > 0)
  14445.     drop procedure sp_check_constraints_rowset
  14446. else
  14447. begin
  14448.     print ''
  14449.     print ''
  14450.     print 'Warning:'
  14451.     print 'you are installing the stored procedures '
  14452.     print 'on a pre 7.0 SQL Server.'
  14453.     print 'Ignore the following errors.'
  14454. end
  14455. go
  14456.  
  14457. /*    Procedure for 7.0 servers and 8.0 servers */
  14458. create procedure sp_check_constraints_rowset
  14459.     (
  14460.     @constraint_name    sysname,
  14461.     @constraint_schema    sysname = null
  14462.     )
  14463. as
  14464.     select
  14465.         CONSTRAINT_CATALOG    = db_name(),
  14466.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14467.         CONSTRAINT_NAME        = c_obj.name,
  14468.         CHECK_CLAUSE        = m.text,    
  14469.         DESCRIPTION            = convert(nvarchar(1), null)
  14470.     from 
  14471.         sysobjects c_obj, syscomments m
  14472.     where
  14473.             c_obj.type = 'C '
  14474.         and c_obj.name    = @constraint_name
  14475.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14476.         and m.id = c_obj.id
  14477.     order by 1,2,3 
  14478. go
  14479. dump tran master with no_log
  14480. go
  14481.  
  14482. create procedure sp_check_constraints_rowset;2
  14483.     (
  14484.     @constraint_schema    sysname = null
  14485.     )
  14486. as
  14487.     select
  14488.         CONSTRAINT_CATALOG    = db_name(),
  14489.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14490.         CONSTRAINT_NAME        = c_obj.name,
  14491.         CHECK_CLAUSE        = m.text,    
  14492.         DESCRIPTION            = convert(nvarchar(1), null)
  14493.     from 
  14494.         sysobjects c_obj, syscomments m
  14495.     where
  14496.             c_obj.type = 'C '
  14497.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14498.         and m.id = c_obj.id
  14499.     order by 1,2,3 
  14500. go
  14501.  
  14502. grant execute on sp_check_constraints_rowset to public
  14503. go
  14504.  
  14505. dump tran master with no_log
  14506. go
  14507.  
  14508. if (charindex('6.00', @@version) > 0)
  14509.     begin
  14510.     if (exists (select * from sysobjects
  14511.             where name = 'sp_check_constraints_rowset' and type = 'P '))
  14512.         begin
  14513.         drop procedure sp_check_constraints_rowset
  14514.         dump tran master with no_log
  14515.         end
  14516.     end
  14517. go
  14518.  
  14519. print ''
  14520. print 'creating sp_check_constbytable_rowset'
  14521. go
  14522.  
  14523. /*    Procedure for 6.50 and earlier servers */
  14524. create procedure sp_check_constbytable_rowset
  14525.     (
  14526.     @table_name            varchar(255), 
  14527.     @table_schema        varchar(255) = null,
  14528.     @constraint_name    varchar(255) = null,
  14529.     @constraint_schema    varchar(255) = null
  14530.     )
  14531. as
  14532.     select
  14533.         TABLE_CATALOG        = db_name(),
  14534.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14535.         TABLE_NAME            = t_obj.name,
  14536.         CONSTRAINT_CATALOG    = db_name(),
  14537.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14538.         CONSTRAINT_NAME        = c_obj.name,
  14539.         CHECK_CLAUSE        = m.text,    
  14540.         DESCRIPTION            = convert(varchar(1),null)
  14541.     from 
  14542.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14543.     where
  14544.             t_obj.name    = @table_name
  14545.         and t_obj.type in ('U ','S ')
  14546.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))    
  14547.         and t_obj.id = c.id
  14548.         and c.constid = c_obj.id
  14549.         and c_obj.type = 'C '
  14550.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14551.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14552.         and c_obj.id = m.id
  14553.     order by 1,2,3,4,5,6 
  14554. go
  14555. dump tran master with no_log
  14556. go
  14557.  
  14558. create procedure sp_check_constbytable_rowset;2
  14559.     (
  14560.     @table_schema        varchar(255) = null,
  14561.     @constraint_name    varchar(255) = null,
  14562.     @constraint_schema    varchar(255) = null
  14563.     )
  14564. as
  14565.     select
  14566.         TABLE_CATALOG        = db_name(),
  14567.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14568.         TABLE_NAME            = t_obj.name,
  14569.         CONSTRAINT_CATALOG    = db_name(),
  14570.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14571.         CONSTRAINT_NAME        = c_obj.name,
  14572.         CHECK_CLAUSE        = m.text,    
  14573.         DESCRIPTION            = convert(varchar(1),null)
  14574.     from 
  14575.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14576.     where
  14577.             t_obj.type in ('U ','S ')
  14578.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14579.         and c.id = t_obj.id
  14580.         and c.constid = c_obj.id
  14581.         and c_obj.type    = 'C '
  14582.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14583.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14584.         and c_obj.id = m.id
  14585.     order by 1,2,3,4,5,6 
  14586. go
  14587. dump tran master with no_log
  14588. go
  14589.  
  14590. if (charindex('7.00', @@version) > 0 or
  14591.     charindex('8.00', @@version) > 0)
  14592.     drop procedure sp_check_constbytable_rowset
  14593. else
  14594. begin
  14595.     print ''
  14596.     print ''
  14597.     print 'Warning:'
  14598.     print 'you are installing the stored procedures '
  14599.     print 'on a pre 8.0 SQL Server.'
  14600.     print 'Ignore the following errors.'
  14601. end
  14602. go
  14603.  
  14604. /*    Procedure for 8.0 server */
  14605. create procedure sp_check_constbytable_rowset
  14606.     (
  14607.     @table_name            sysname, 
  14608.     @table_schema        sysname = null,
  14609.     @constraint_name    sysname = null,
  14610.     @constraint_schema    sysname = null
  14611.     )
  14612. as
  14613.     select
  14614.         TABLE_CATALOG        = db_name(),
  14615.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14616.         TABLE_NAME            = t_obj.name,
  14617.         CONSTRAINT_CATALOG    = db_name(),
  14618.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14619.         CONSTRAINT_NAME        = c_obj.name,
  14620.         CHECK_CLAUSE        = m.text,    
  14621.         DESCRIPTION            = convert(nvarchar(1), null)
  14622.  
  14623.     from 
  14624.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14625.     where
  14626.             t_obj.name    = @table_name
  14627.         and t_obj.type in ('U ','S ')
  14628.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14629.         and c_obj.parent_obj = t_obj.id
  14630.         and c_obj.type = 'C '
  14631.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14632.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14633.         and m.id = c_obj.id
  14634. order by 1,2,3,4,5,6 
  14635. go
  14636. dump tran master with no_log
  14637. go
  14638. create procedure sp_check_constbytable_rowset;2
  14639.     (
  14640.     @table_schema        sysname = null,
  14641.     @constraint_name    sysname = null,
  14642.     @constraint_schema    sysname = null
  14643.     )
  14644. as
  14645.     select
  14646.         TABLE_CATALOG        = db_name(),
  14647.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14648.         TABLE_NAME            = t_obj.name,
  14649.         CONSTRAINT_CATALOG    = db_name(),
  14650.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14651.         CONSTRAINT_NAME        = c_obj.name,
  14652.         CHECK_CLAUSE        = m.text,    
  14653.         DESCRIPTION            = convert(nvarchar(1), null)
  14654.     from 
  14655.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14656.     where
  14657.             t_obj.type in ('U ','S ')
  14658.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14659.         and c_obj.parent_obj = t_obj.id
  14660.         and c_obj.type = 'C '
  14661.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14662.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14663.         and m.id = c_obj.id
  14664.     order by 1,2,3,4,5,6 
  14665. go
  14666. dump tran master with no_log
  14667. go
  14668.  
  14669. grant execute on sp_check_constbytable_rowset to public
  14670. go
  14671.  
  14672. dump tran master with no_log
  14673. go
  14674. if (charindex('6.00', @@version) > 0)
  14675.     begin
  14676.     if (exists (select * from sysobjects
  14677.             where name = 'sp_check_constbytable_rowset' and type = 'P '))
  14678.         begin
  14679.         drop procedure sp_check_constbytable_rowset
  14680.         dump tran master with no_log
  14681.         end
  14682.     end
  14683. go
  14684.  
  14685.  
  14686. print ''
  14687. print 'creating sp_foreign_keys_rowset'
  14688. go
  14689.  
  14690. /*    Procedure for 6.0 and 6.5 server */
  14691. CREATE PROCEDURE sp_foreign_keys_rowset
  14692.     (
  14693.      @pk_table_name        varchar(255) = null,
  14694.     @pk_table_schema    varchar(255) = null,
  14695.     @pk_table_catalog    varchar(255) = null,
  14696.     @fk_table_name        varchar(255) = null,
  14697.     @fk_table_schema    varchar(255) = null,
  14698.     @fk_table_catalog    varchar(255) = null
  14699.     )
  14700. as
  14701.     BEGIN
  14702.     select
  14703.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14704.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14705.         PK_TABLE_NAME         = o1.name,
  14706.         PK_COLUMN_NAME         = c1.name,
  14707.         PK_COLUMN_GUID        = convert(binary(16),null),
  14708.         PK_COLUMN_PROPID    = convert(int,null),
  14709.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14710.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14711.         FK_TABLE_NAME         = o2.name,
  14712.         FK_COLUMN_NAME         = c2.name,
  14713.         FK_COLUMN_GUID        = convert(binary(16),null),
  14714.         FK_COLUMN_PROPID    = convert(int,null),
  14715.         ORDINAL                = convert(int,1),
  14716.         UPDATE_RULE            = 'NO ACTION',
  14717.         DELETE_RULE         = 'NO ACTION',
  14718.         PK_NAME                = i.name,
  14719.         FK_NAME                = object_name(r.constid),
  14720.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14721.     from
  14722.         sysobjects o1, sysobjects o2,
  14723.         syscolumns c1, syscolumns c2,
  14724.         sysreferences r, sysindexes i, sysusers u
  14725.     where    
  14726.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14727.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14728.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14729.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14730.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14731.     and    o1.id = r.rkeyid
  14732.     and    o1.id = c1.id
  14733.     and    c1.colid = r.rkey1
  14734.     and r.fkeyid = o2.id
  14735.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14736.     and    o2.id = c2.id    
  14737.     and    c2.colid = r.fkey1
  14738.     and i.id = r.rkeyid
  14739.     and i.indid = r.rkeyindid
  14740.     and u.uid = user_id()
  14741.     and ( suser_id() = 1     /* User is the System Administrator */
  14742.     or (
  14743.         o1.uid = user_id()     /* User created the object */
  14744.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14745.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14746.             from sysprotects p
  14747.             where p.id = o1.id
  14748.             /* get rows for public,current user,user's group */
  14749.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14750.             /* check for SELECT,EXECUTE privilege */
  14751.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14752.             ) = 1    /* final magic...compare Grants      */
  14753.         )
  14754.     and (
  14755.         o2.uid = user_id()     /* User created the object */
  14756.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14757.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14758.             from sysprotects p
  14759.             where p.id = o2.id
  14760.             /* get rows for public,current user,user's group */
  14761.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14762.             /* check for SELECT,EXECUTE privilege */
  14763.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14764.             ) = 1    /* final magic...compare Grants      */
  14765.         )
  14766.     )
  14767.     union all
  14768.     select
  14769.         db_name(r.rkeydbid),
  14770.         user_name(o1.uid),
  14771.         o1.name,
  14772.         c1.name,
  14773.         convert(binary(16),null),
  14774.         convert(int,null),
  14775.         db_name(r.fkeydbid),
  14776.         user_name(o2.uid),
  14777.         o2.name,
  14778.         c2.name,
  14779.         convert(binary(16),null),
  14780.         convert(int,null),
  14781.         convert(int,2),
  14782.         'NO ACTION',
  14783.         'NO ACTION',
  14784.         i.name,
  14785.         object_name(r.constid),
  14786.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14787.     from
  14788.         sysobjects o1, sysobjects o2,
  14789.         syscolumns c1, syscolumns c2,
  14790.         sysreferences r, sysindexes i, sysusers u
  14791.     where    
  14792.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14793.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14794.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14795.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14796.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14797.     and    o1.id = r.rkeyid
  14798.     and    r.keycnt >= 2
  14799.     and    o1.id = c1.id
  14800.     and    c1.colid = r.rkey2
  14801.     and r.fkeyid = o2.id
  14802.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14803.     and    o2.id = c2.id    
  14804.     and    c2.colid = r.fkey2
  14805.     and i.id = r.rkeyid
  14806.     and i.indid = r.rkeyindid
  14807.     and u.uid = user_id()
  14808.     and ( suser_id() = 1     /* User is the System Administrator */
  14809.     or (
  14810.         o1.uid = user_id()     /* User created the object */
  14811.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14812.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14813.             from sysprotects p
  14814.             where p.id = o1.id
  14815.             /* get rows for public,current user,user's group */
  14816.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14817.             /* check for SELECT,EXECUTE privilege */
  14818.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14819.             ) = 1    /* final magic...compare Grants      */
  14820.         )
  14821.     and (
  14822.         o2.uid = user_id()     /* User created the object */
  14823.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14824.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14825.             from sysprotects p
  14826.             where p.id = o2.id
  14827.             /* get rows for public,current user,user's group */
  14828.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14829.             /* check for SELECT,EXECUTE privilege */
  14830.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14831.             ) = 1    /* final magic...compare Grants      */
  14832.         )
  14833.     )
  14834.     union all
  14835.     select
  14836.         db_name(r.rkeydbid),
  14837.         user_name(o1.uid),
  14838.         o1.name,
  14839.         c1.name,
  14840.         convert(binary(16),null),
  14841.         convert(int,null),
  14842.         db_name(r.fkeydbid),
  14843.         user_name(o2.uid),
  14844.         o2.name,
  14845.         c2.name,
  14846.         convert(binary(16),null),
  14847.         convert(int,null),
  14848.         convert(int,3),
  14849.         'NO ACTION',
  14850.         'NO ACTION',
  14851.         i.name,
  14852.         object_name(r.constid),
  14853.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14854.     from
  14855.         sysobjects o1, sysobjects o2,
  14856.         syscolumns c1, syscolumns c2,
  14857.         sysreferences r, sysindexes i, sysusers u
  14858.     where    
  14859.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14860.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14861.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14862.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14863.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14864.     and    o1.id = r.rkeyid
  14865.     and    r.keycnt >= 3
  14866.     and    o1.id = c1.id
  14867.     and    c1.colid = r.rkey3
  14868.     and r.fkeyid = o2.id
  14869.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14870.     and    o2.id = c2.id    
  14871.     and    c2.colid = r.fkey3
  14872.     and i.id = r.rkeyid
  14873.     and i.indid = r.rkeyindid
  14874.     and u.uid = user_id()
  14875.     and ( suser_id() = 1     /* User is the System Administrator */
  14876.     or (
  14877.         o1.uid = user_id()     /* User created the object */
  14878.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14879.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14880.             from sysprotects p
  14881.             where p.id = o1.id
  14882.             /* get rows for public,current user,user's group */
  14883.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14884.             /* check for SELECT,EXECUTE privilege */
  14885.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14886.             ) = 1    /* final magic...compare Grants      */
  14887.         )
  14888.     and (
  14889.         o2.uid = user_id()     /* User created the object */
  14890.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14891.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14892.             from sysprotects p
  14893.             where p.id = o2.id
  14894.             /* get rows for public,current user,user's group */
  14895.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14896.             /* check for SELECT,EXECUTE privilege */
  14897.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14898.             ) = 1    /* final magic...compare Grants      */
  14899.         )
  14900.     )
  14901.     union all
  14902.     select
  14903.         db_name(r.rkeydbid),
  14904.         user_name(o1.uid),
  14905.         o1.name,
  14906.         c1.name,
  14907.         convert(binary(16),null),
  14908.         convert(int,null),
  14909.         db_name(r.fkeydbid),
  14910.         user_name(o2.uid),
  14911.         o2.name,
  14912.         c2.name,
  14913.         convert(binary(16),null),
  14914.         convert(int,null),
  14915.         convert(int,4),
  14916.         'NO ACTION',
  14917.         'NO ACTION',
  14918.         i.name,
  14919.         object_name(r.constid),
  14920.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14921.     from
  14922.         sysobjects o1, sysobjects o2,
  14923.         syscolumns c1, syscolumns c2,
  14924.         sysreferences r, sysindexes i, sysusers u
  14925.     where    
  14926.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14927.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14928.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14929.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14930.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14931.     and    o1.id = r.rkeyid
  14932.     and    r.keycnt >= 4
  14933.     and    o1.id = c1.id
  14934.     and    c1.colid = r.rkey4
  14935.     and r.fkeyid = o2.id
  14936.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14937.     and    o2.id = c2.id    
  14938.     and    c2.colid = r.fkey4
  14939.     and i.id = r.rkeyid
  14940.     and i.indid = r.rkeyindid
  14941.     and u.uid = user_id()
  14942.     and ( suser_id() = 1     /* User is the System Administrator */
  14943.     or (
  14944.         o1.uid = user_id()     /* User created the object */
  14945.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14946.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14947.             from sysprotects p
  14948.             where p.id = o1.id
  14949.             /* get rows for public,current user,user's group */
  14950.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14951.             /* check for SELECT,EXECUTE privilege */
  14952.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14953.             ) = 1    /* final magic...compare Grants      */
  14954.         )
  14955.     and (
  14956.         o2.uid = user_id()     /* User created the object */
  14957.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14958.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14959.             from sysprotects p
  14960.             where p.id = o2.id
  14961.             /* get rows for public,current user,user's group */
  14962.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14963.             /* check for SELECT,EXECUTE privilege */
  14964.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14965.             ) = 1    /* final magic...compare Grants      */
  14966.         )
  14967.     )
  14968.     union all
  14969.     select
  14970.         db_name(r.rkeydbid),
  14971.         user_name(o1.uid),
  14972.         o1.name,
  14973.         c1.name,
  14974.         convert(binary(16),null),
  14975.         convert(int,null),
  14976.         db_name(r.fkeydbid),
  14977.         user_name(o2.uid),
  14978.         o2.name,
  14979.         c2.name,
  14980.         convert(binary(16),null),
  14981.         convert(int,null),
  14982.         convert(int,5),
  14983.         'NO ACTION',
  14984.         'NO ACTION',
  14985.         i.name,
  14986.         object_name(r.constid),
  14987.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14988.     from
  14989.         sysobjects o1, sysobjects o2,
  14990.         syscolumns c1, syscolumns c2,
  14991.         sysreferences r, sysindexes i, sysusers u
  14992.     where    
  14993.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14994.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14995.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14996.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14997.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14998.     and    o1.id = r.rkeyid
  14999.     and    r.keycnt >= 5
  15000.     and    o1.id = c1.id
  15001.     and    c1.colid = r.rkey5
  15002.     and r.fkeyid = o2.id
  15003.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15004.     and    o2.id = c2.id    
  15005.     and    c2.colid = r.fkey5
  15006.     and i.id = r.rkeyid
  15007.     and i.indid = r.rkeyindid
  15008.     and u.uid = user_id()
  15009.     and ( suser_id() = 1     /* User is the System Administrator */
  15010.     or (
  15011.         o1.uid = user_id()     /* User created the object */
  15012.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15013.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15014.             from sysprotects p
  15015.             where p.id = o1.id
  15016.             /* get rows for public,current user,user's group */
  15017.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15018.             /* check for SELECT,EXECUTE privilege */
  15019.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15020.             ) = 1    /* final magic...compare Grants      */
  15021.         )
  15022.     and (
  15023.         o2.uid = user_id()     /* User created the object */
  15024.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15025.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15026.             from sysprotects p
  15027.             where p.id = o2.id
  15028.             /* get rows for public,current user,user's group */
  15029.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15030.             /* check for SELECT,EXECUTE privilege */
  15031.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15032.             ) = 1    /* final magic...compare Grants      */
  15033.         )
  15034.     )
  15035.     union all
  15036.     select
  15037.         db_name(r.rkeydbid),
  15038.         user_name(o1.uid),
  15039.         o1.name,
  15040.         c1.name,
  15041.         convert(binary(16),null),
  15042.         convert(int,null),
  15043.         db_name(r.fkeydbid),
  15044.         user_name(o2.uid),
  15045.         o2.name,
  15046.         c2.name,
  15047.         convert(binary(16),null),
  15048.         convert(int,null),
  15049.         convert(int,6),
  15050.         'NO ACTION',
  15051.         'NO ACTION',
  15052.         i.name,
  15053.         object_name(r.constid),
  15054.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15055.     from
  15056.         sysobjects o1, sysobjects o2,
  15057.         syscolumns c1, syscolumns c2,
  15058.         sysreferences r, sysindexes i, sysusers u
  15059.     where    
  15060.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15061.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15062.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15063.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15064.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15065.     and    o1.id = r.rkeyid
  15066.     and    r.keycnt >= 6
  15067.     and    o1.id = c1.id
  15068.     and    c1.colid = r.rkey6
  15069.     and r.fkeyid = o2.id
  15070.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15071.     and    o2.id = c2.id    
  15072.     and    c2.colid = r.fkey6
  15073.     and i.id = r.rkeyid
  15074.     and i.indid = r.rkeyindid
  15075.     and u.uid = user_id()
  15076.     and ( suser_id() = 1     /* User is the System Administrator */
  15077.     or (
  15078.         o1.uid = user_id()     /* User created the object */
  15079.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15080.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15081.             from sysprotects p
  15082.             where p.id = o1.id
  15083.             /* get rows for public,current user,user's group */
  15084.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15085.             /* check for SELECT,EXECUTE privilege */
  15086.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15087.             ) = 1    /* final magic...compare Grants      */
  15088.         )
  15089.     and (
  15090.         o2.uid = user_id()     /* User created the object */
  15091.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15092.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15093.             from sysprotects p
  15094.             where p.id = o2.id
  15095.             /* get rows for public,current user,user's group */
  15096.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15097.             /* check for SELECT,EXECUTE privilege */
  15098.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15099.             ) = 1    /* final magic...compare Grants      */
  15100.         )
  15101.     )
  15102.     union all
  15103.     select
  15104.         db_name(r.rkeydbid),
  15105.         user_name(o1.uid),
  15106.         o1.name,
  15107.         c1.name,
  15108.         convert(binary(16),null),
  15109.         convert(int,null),
  15110.         db_name(r.fkeydbid),
  15111.         user_name(o2.uid),
  15112.         o2.name,
  15113.         c2.name,
  15114.         convert(binary(16),null),
  15115.         convert(int,null),
  15116.         convert(int,7),
  15117.         'NO ACTION',
  15118.         'NO ACTION',
  15119.         i.name,
  15120.         object_name(r.constid),
  15121.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15122.     from
  15123.         sysobjects o1, sysobjects o2,
  15124.         syscolumns c1, syscolumns c2,
  15125.         sysreferences r, sysindexes i, sysusers u
  15126.     where    
  15127.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15128.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15129.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15130.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15131.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15132.     and    o1.id = r.rkeyid
  15133.     and    r.keycnt >= 7
  15134.     and    o1.id = c1.id
  15135.     and    c1.colid = r.rkey7
  15136.     and r.fkeyid = o2.id
  15137.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15138.     and    o2.id = c2.id    
  15139.     and    c2.colid = r.fkey7
  15140.     and i.id = r.rkeyid
  15141.     and i.indid = r.rkeyindid
  15142.     and u.uid = user_id()
  15143.     and ( suser_id() = 1     /* User is the System Administrator */
  15144.     or (
  15145.         o1.uid = user_id()     /* User created the object */
  15146.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15147.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15148.             from sysprotects p
  15149.             where p.id = o1.id
  15150.             /* get rows for public,current user,user's group */
  15151.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15152.             /* check for SELECT,EXECUTE privilege */
  15153.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15154.             ) = 1    /* final magic...compare Grants      */
  15155.         )
  15156.     and (
  15157.         o2.uid = user_id()     /* User created the object */
  15158.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15159.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15160.             from sysprotects p
  15161.             where p.id = o2.id
  15162.             /* get rows for public,current user,user's group */
  15163.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15164.             /* check for SELECT,EXECUTE privilege */
  15165.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15166.             ) = 1    /* final magic...compare Grants      */
  15167.         )
  15168.     )
  15169.     union all
  15170.     select
  15171.         db_name(r.rkeydbid),
  15172.         user_name(o1.uid),
  15173.         o1.name,
  15174.         c1.name,
  15175.         convert(binary(16),null),
  15176.         convert(int,null),
  15177.         db_name(r.fkeydbid),
  15178.         user_name(o2.uid),
  15179.         o2.name,
  15180.         c2.name,
  15181.         convert(binary(16),null),
  15182.         convert(int,null),
  15183.         convert(int,8),
  15184.         'NO ACTION',
  15185.         'NO ACTION',
  15186.         i.name,
  15187.         object_name(r.constid),
  15188.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15189.     from
  15190.         sysobjects o1, sysobjects o2,
  15191.         syscolumns c1, syscolumns c2,
  15192.         sysreferences r, sysindexes i, sysusers u
  15193.     where    
  15194.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15195.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15196.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15197.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15198.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15199.     and    o1.id = r.rkeyid
  15200.     and    r.keycnt >= 8
  15201.     and    o1.id = c1.id
  15202.     and    c1.colid = r.rkey8
  15203.     and r.fkeyid = o2.id
  15204.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15205.     and    o2.id = c2.id    
  15206.     and    c2.colid = r.fkey8
  15207.     and i.id = r.rkeyid
  15208.     and i.indid = r.rkeyindid
  15209.     and u.uid = user_id()
  15210.     and ( suser_id() = 1     /* User is the System Administrator */
  15211.     or (
  15212.         o1.uid = user_id()     /* User created the object */
  15213.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15214.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15215.             from sysprotects p
  15216.             where p.id = o1.id
  15217.             /* get rows for public,current user,user's group */
  15218.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15219.             /* check for SELECT,EXECUTE privilege */
  15220.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15221.             ) = 1    /* final magic...compare Grants      */
  15222.         )
  15223.     and (
  15224.         o2.uid = user_id()     /* User created the object */
  15225.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15226.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15227.             from sysprotects p
  15228.             where p.id = o2.id
  15229.             /* get rows for public,current user,user's group */
  15230.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15231.             /* check for SELECT,EXECUTE privilege */
  15232.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15233.             ) = 1    /* final magic...compare Grants      */
  15234.         )
  15235.     )
  15236.     union all
  15237.     select
  15238.         db_name(r.rkeydbid),
  15239.         user_name(o1.uid),
  15240.         o1.name,
  15241.         c1.name,
  15242.         convert(binary(16),null),
  15243.         convert(int,null),
  15244.         db_name(r.fkeydbid),
  15245.         user_name(o2.uid),
  15246.         o2.name,
  15247.         c2.name,
  15248.         convert(binary(16),null),
  15249.         convert(int,null),
  15250.         convert(int,9),
  15251.         'NO ACTION',
  15252.         'NO ACTION',
  15253.         i.name,
  15254.         object_name(r.constid),
  15255.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15256.     from
  15257.         sysobjects o1, sysobjects o2,
  15258.         syscolumns c1, syscolumns c2,
  15259.         sysreferences r, sysindexes i, sysusers u
  15260.     where    
  15261.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15262.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15263.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15264.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15265.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15266.     and    o1.id = r.rkeyid
  15267.     and    r.keycnt >= 9
  15268.     and    o1.id = c1.id
  15269.     and    c1.colid = r.rkey9
  15270.     and r.fkeyid = o2.id
  15271.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15272.     and    o2.id = c2.id    
  15273.     and    c2.colid = r.fkey9
  15274.     and i.id = r.rkeyid
  15275.     and i.indid = r.rkeyindid
  15276.     and u.uid = user_id()
  15277.     and ( suser_id() = 1     /* User is the System Administrator */
  15278.     or (
  15279.         o1.uid = user_id()     /* User created the object */
  15280.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15281.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15282.             from sysprotects p
  15283.             where p.id = o1.id
  15284.             /* get rows for public,current user,user's group */
  15285.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15286.             /* check for SELECT,EXECUTE privilege */
  15287.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15288.             ) = 1    /* final magic...compare Grants      */
  15289.         )
  15290.     and (
  15291.         o2.uid = user_id()     /* User created the object */
  15292.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15293.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15294.             from sysprotects p
  15295.             where p.id = o2.id
  15296.             /* get rows for public,current user,user's group */
  15297.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15298.             /* check for SELECT,EXECUTE privilege */
  15299.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15300.             ) = 1    /* final magic...compare Grants      */
  15301.         )
  15302.     )
  15303.     union all
  15304.     select
  15305.         db_name(r.rkeydbid),
  15306.         user_name(o1.uid),
  15307.         o1.name,
  15308.         c1.name,
  15309.         convert(binary(16),null),
  15310.         convert(int,null),
  15311.         db_name(r.fkeydbid),
  15312.         user_name(o2.uid),
  15313.         o2.name,
  15314.         c2.name,
  15315.         convert(binary(16),null),
  15316.         convert(int,null),
  15317.         convert(int,10),
  15318.         'NO ACTION',
  15319.         'NO ACTION',
  15320.         i.name,
  15321.         object_name(r.constid),
  15322.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15323.     from
  15324.         sysobjects o1, sysobjects o2,
  15325.         syscolumns c1, syscolumns c2,
  15326.         sysreferences r, sysindexes i, sysusers u
  15327.     where    
  15328.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15329.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15330.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15331.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15332.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15333.     and    o1.id = r.rkeyid
  15334.     and    r.keycnt >= 10
  15335.     and    o1.id = c1.id
  15336.     and    c1.colid = r.rkey10
  15337.     and r.fkeyid = o2.id
  15338.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15339.     and    o2.id = c2.id    
  15340.     and    c2.colid = r.fkey10
  15341.     and i.id = r.rkeyid
  15342.     and i.indid = r.rkeyindid
  15343.     and u.uid = user_id()
  15344.     and ( suser_id() = 1     /* User is the System Administrator */
  15345.     or (
  15346.         o1.uid = user_id()     /* User created the object */
  15347.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15348.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15349.             from sysprotects p
  15350.             where p.id = o1.id
  15351.             /* get rows for public,current user,user's group */
  15352.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15353.             /* check for SELECT,EXECUTE privilege */
  15354.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15355.             ) = 1    /* final magic...compare Grants      */
  15356.         )
  15357.     and (
  15358.         o2.uid = user_id()     /* User created the object */
  15359.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15360.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15361.             from sysprotects p
  15362.             where p.id = o2.id
  15363.             /* get rows for public,current user,user's group */
  15364.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15365.             /* check for SELECT,EXECUTE privilege */
  15366.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15367.             ) = 1    /* final magic...compare Grants      */
  15368.         )
  15369.     )
  15370.     union all
  15371.     select
  15372.         db_name(r.rkeydbid),
  15373.         user_name(o1.uid),
  15374.         o1.name,
  15375.         c1.name,
  15376.         convert(binary(16),null),
  15377.         convert(int,null),
  15378.         db_name(r.fkeydbid),
  15379.         user_name(o2.uid),
  15380.         o2.name,
  15381.         c2.name,
  15382.         convert(binary(16),null),
  15383.         convert(int,null),
  15384.         convert(int,11),
  15385.         'NO ACTION',
  15386.         'NO ACTION',
  15387.         i.name,
  15388.         object_name(r.constid),
  15389.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15390.     from
  15391.         sysobjects o1, sysobjects o2,
  15392.         syscolumns c1, syscolumns c2,
  15393.         sysreferences r, sysindexes i, sysusers u
  15394.     where    
  15395.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15396.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15397.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15398.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15399.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15400.     and    o1.id = r.rkeyid
  15401.     and    r.keycnt >= 11
  15402.     and    o1.id = c1.id
  15403.     and    c1.colid = r.rkey11
  15404.     and r.fkeyid = o2.id
  15405.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15406.     and    o2.id = c2.id    
  15407.     and    c2.colid = r.fkey11
  15408.     and i.id = r.rkeyid
  15409.     and i.indid = r.rkeyindid
  15410.     and u.uid = user_id()
  15411.     and ( suser_id() = 1     /* User is the System Administrator */
  15412.     or (
  15413.         o1.uid = user_id()     /* User created the object */
  15414.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15415.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15416.             from sysprotects p
  15417.             where p.id = o1.id
  15418.             /* get rows for public,current user,user's group */
  15419.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15420.             /* check for SELECT,EXECUTE privilege */
  15421.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15422.             ) = 1    /* final magic...compare Grants      */
  15423.         )
  15424.     and (
  15425.         o2.uid = user_id()     /* User created the object */
  15426.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15427.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15428.             from sysprotects p
  15429.             where p.id = o2.id
  15430.             /* get rows for public,current user,user's group */
  15431.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15432.             /* check for SELECT,EXECUTE privilege */
  15433.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15434.             ) = 1    /* final magic...compare Grants      */
  15435.         )
  15436.     )
  15437.     union all
  15438.     select
  15439.         db_name(r.rkeydbid),
  15440.         user_name(o1.uid),
  15441.         o1.name,
  15442.         c1.name,
  15443.         convert(binary(16),null),
  15444.         convert(int,null),
  15445.         db_name(r.fkeydbid),
  15446.         user_name(o2.uid),
  15447.         o2.name,
  15448.         c2.name,
  15449.         convert(binary(16),null),
  15450.         convert(int,null),
  15451.         convert(int,12),
  15452.         'NO ACTION',
  15453.         'NO ACTION',
  15454.         i.name,
  15455.         object_name(r.constid),
  15456.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15457.     from
  15458.         sysobjects o1, sysobjects o2,
  15459.         syscolumns c1, syscolumns c2,
  15460.         sysreferences r, sysindexes i, sysusers u
  15461.     where    
  15462.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15463.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15464.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15465.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15466.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15467.     and    o1.id = r.rkeyid
  15468.     and    r.keycnt >= 12
  15469.     and    o1.id = c1.id
  15470.     and    c1.colid = r.rkey12
  15471.     and r.fkeyid = o2.id
  15472.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15473.     and    o2.id = c2.id    
  15474.     and    c2.colid = r.fkey12
  15475.     and i.id = r.rkeyid
  15476.     and i.indid = r.rkeyindid
  15477.     and u.uid = user_id()
  15478.     and ( suser_id() = 1     /* User is the System Administrator */
  15479.     or (
  15480.         o1.uid = user_id()     /* User created the object */
  15481.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15482.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15483.             from sysprotects p
  15484.             where p.id = o1.id
  15485.             /* get rows for public,current user,user's group */
  15486.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15487.             /* check for SELECT,EXECUTE privilege */
  15488.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15489.             ) = 1    /* final magic...compare Grants      */
  15490.         )
  15491.     and (
  15492.         o2.uid = user_id()     /* User created the object */
  15493.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15494.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15495.             from sysprotects p
  15496.             where p.id = o2.id
  15497.             /* get rows for public,current user,user's group */
  15498.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15499.             /* check for SELECT,EXECUTE privilege */
  15500.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15501.             ) = 1    /* final magic...compare Grants      */
  15502.         )
  15503.     )
  15504.     union all
  15505.     select
  15506.         db_name(r.rkeydbid),
  15507.         user_name(o1.uid),
  15508.         o1.name,
  15509.         c1.name,
  15510.         convert(binary(16),null),
  15511.         convert(int,null),
  15512.         db_name(r.fkeydbid),
  15513.         user_name(o2.uid),
  15514.         o2.name,
  15515.         c2.name,
  15516.         convert(binary(16),null),
  15517.         convert(int,null),
  15518.         convert(int,13),
  15519.         'NO ACTION',
  15520.         'NO ACTION',
  15521.         i.name,
  15522.         object_name(r.constid),
  15523.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15524.     from
  15525.         sysobjects o1, sysobjects o2,
  15526.         syscolumns c1, syscolumns c2,
  15527.         sysreferences r, sysindexes i, sysusers u
  15528.     where    
  15529.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15530.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15531.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15532.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15533.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15534.     and    o1.id = r.rkeyid
  15535.     and    r.keycnt >= 13
  15536.     and    o1.id = c1.id
  15537.     and    c1.colid = r.rkey13
  15538.     and r.fkeyid = o2.id
  15539.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15540.     and    o2.id = c2.id    
  15541.     and    c2.colid = r.fkey13
  15542.     and i.id = r.rkeyid
  15543.     and i.indid = r.rkeyindid
  15544.     and u.uid = user_id()
  15545.     and ( suser_id() = 1     /* User is the System Administrator */
  15546.     or (
  15547.         o1.uid = user_id()     /* User created the object */
  15548.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15549.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15550.             from sysprotects p
  15551.             where p.id = o1.id
  15552.             /* get rows for public,current user,user's group */
  15553.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15554.             /* check for SELECT,EXECUTE privilege */
  15555.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15556.             ) = 1    /* final magic...compare Grants      */
  15557.         )
  15558.     and (
  15559.         o2.uid = user_id()     /* User created the object */
  15560.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15561.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15562.             from sysprotects p
  15563.             where p.id = o2.id
  15564.             /* get rows for public,current user,user's group */
  15565.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15566.             /* check for SELECT,EXECUTE privilege */
  15567.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15568.             ) = 1    /* final magic...compare Grants      */
  15569.         )
  15570.     )
  15571.     union all
  15572.     select
  15573.         db_name(r.rkeydbid),
  15574.         user_name(o1.uid),
  15575.         o1.name,
  15576.         c1.name,
  15577.         convert(binary(16),null),
  15578.         convert(int,null),
  15579.         db_name(r.fkeydbid),
  15580.         user_name(o2.uid),
  15581.         o2.name,
  15582.         c2.name,
  15583.         convert(binary(16),null),
  15584.         convert(int,null),
  15585.         convert(int,14),
  15586.         'NO ACTION',
  15587.         'NO ACTION',
  15588.         i.name,
  15589.         object_name(r.constid),
  15590.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15591.     from
  15592.         sysobjects o1, sysobjects o2,
  15593.         syscolumns c1, syscolumns c2,
  15594.         sysreferences r, sysindexes i, sysusers u
  15595.     where    
  15596.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15597.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15598.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15599.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15600.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15601.     and    o1.id = r.rkeyid
  15602.     and    r.keycnt >= 14
  15603.     and    o1.id = c1.id
  15604.     and    c1.colid = r.rkey14
  15605.     and r.fkeyid = o2.id
  15606.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15607.     and    o2.id = c2.id    
  15608.     and    c2.colid = r.fkey14
  15609.     and i.id = r.rkeyid
  15610.     and i.indid = r.rkeyindid
  15611.     and u.uid = user_id()
  15612.     and ( suser_id() = 1     /* User is the System Administrator */
  15613.     or (
  15614.         o1.uid = user_id()     /* User created the object */
  15615.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15616.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15617.             from sysprotects p
  15618.             where p.id = o1.id
  15619.             /* get rows for public,current user,user's group */
  15620.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15621.             /* check for SELECT,EXECUTE privilege */
  15622.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15623.             ) = 1    /* final magic...compare Grants      */
  15624.         )
  15625.     and (
  15626.         o2.uid = user_id()     /* User created the object */
  15627.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15628.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15629.             from sysprotects p
  15630.             where p.id = o2.id
  15631.             /* get rows for public,current user,user's group */
  15632.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15633.             /* check for SELECT,EXECUTE privilege */
  15634.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15635.             ) = 1    /* final magic...compare Grants      */
  15636.         )
  15637.     )
  15638.     union all
  15639.     select
  15640.         db_name(r.rkeydbid),
  15641.         user_name(o1.uid),
  15642.         o1.name,
  15643.         c1.name,
  15644.         convert(binary(16),null),
  15645.         convert(int,null),
  15646.         db_name(r.fkeydbid),
  15647.         user_name(o2.uid),
  15648.         o2.name,
  15649.         c2.name,
  15650.         convert(binary(16),null),
  15651.         convert(int,null),
  15652.         convert(int,15),
  15653.         'NO ACTION',
  15654.         'NO ACTION',
  15655.         i.name,
  15656.         object_name(r.constid),
  15657.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15658.     from
  15659.         sysobjects o1, sysobjects o2,
  15660.         syscolumns c1, syscolumns c2,
  15661.         sysreferences r, sysindexes i, sysusers u
  15662.     where    
  15663.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15664.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15665.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15666.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15667.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15668.     and    o1.id = r.rkeyid
  15669.     and    r.keycnt >= 15
  15670.     and    o1.id = c1.id
  15671.     and    c1.colid = r.rkey15
  15672.     and r.fkeyid = o2.id
  15673.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15674.     and    o2.id = c2.id    
  15675.     and    c2.colid = r.fkey15
  15676.     and i.id = r.rkeyid
  15677.     and i.indid = r.rkeyindid
  15678.     and u.uid = user_id()
  15679.     and ( suser_id() = 1     /* User is the System Administrator */
  15680.     or (
  15681.         o1.uid = user_id()     /* User created the object */
  15682.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15683.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15684.             from sysprotects p
  15685.             where p.id = o1.id
  15686.             /* get rows for public,current user,user's group */
  15687.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15688.             /* check for SELECT,EXECUTE privilege */
  15689.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15690.             ) = 1    /* final magic...compare Grants      */
  15691.         )
  15692.     and (
  15693.         o2.uid = user_id()     /* User created the object */
  15694.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15695.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15696.             from sysprotects p
  15697.             where p.id = o2.id
  15698.             /* get rows for public,current user,user's group */
  15699.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15700.             /* check for SELECT,EXECUTE privilege */
  15701.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15702.             ) = 1    /* final magic...compare Grants      */
  15703.         )
  15704.     )
  15705.     union all
  15706.     select
  15707.         db_name(r.rkeydbid),
  15708.         user_name(o1.uid),
  15709.         o1.name,
  15710.         c1.name,
  15711.         convert(binary(16),null),
  15712.         convert(int,null),
  15713.         db_name(r.fkeydbid),
  15714.         user_name(o2.uid),
  15715.         o2.name,
  15716.         c2.name,
  15717.         convert(binary(16),null),
  15718.         convert(int,null),
  15719.         convert(int,16),
  15720.         'NO ACTION',
  15721.         'NO ACTION',
  15722.         i.name,
  15723.         object_name(r.constid),
  15724.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15725.     from
  15726.         sysobjects o1, sysobjects o2,
  15727.         syscolumns c1, syscolumns c2,
  15728.         sysreferences r, sysindexes i, sysusers u
  15729.     where    
  15730.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15731.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15732.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15733.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15734.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15735.     and    o1.id = r.rkeyid
  15736.     and    o1.id = c1.id
  15737.     and    r.keycnt >= 16
  15738.     and    c1.colid = r.rkey16
  15739.     and r.fkeyid = o2.id
  15740.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15741.     and    o2.id = c2.id    
  15742.     and    c2.colid = r.fkey16
  15743.     and i.id = r.rkeyid
  15744.     and i.indid = r.rkeyindid
  15745.     and u.uid = user_id()
  15746.     and ( suser_id() = 1     /* User is the System Administrator */
  15747.     or (
  15748.         o1.uid = user_id()     /* User created the object */
  15749.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15750.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15751.             from sysprotects p
  15752.             where p.id = o1.id
  15753.             /* get rows for public,current user,user's group */
  15754.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15755.             /* check for SELECT,EXECUTE privilege */
  15756.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15757.             ) = 1    /* final magic...compare Grants      */
  15758.         )
  15759.     and (
  15760.         o2.uid = user_id()     /* User created the object */
  15761.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15762.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15763.             from sysprotects p
  15764.             where p.id = o2.id
  15765.             /* get rows for public,current user,user's group */
  15766.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15767.             /* check for SELECT,EXECUTE privilege */
  15768.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15769.             ) = 1    /* final magic...compare Grants      */
  15770.         )
  15771.     )
  15772.     order by 8,9,2,3,13
  15773.     END
  15774. go
  15775. dump tran master with no_log
  15776. go
  15777. CREATE PROCEDURE sp_foreign_keys_rowset;2
  15778.     (
  15779.        @handle            int output,
  15780.        @scrollopt        int output,
  15781.     @ccopt            int output,
  15782.     @rows            int output,
  15783.      @pk_table_name        varchar(255) = null,
  15784.     @pk_table_schema    varchar(255) = null,
  15785.     @pk_table_catalog    varchar(255) = null,
  15786.     @fk_table_name        varchar(255) = null,
  15787.     @fk_table_schema    varchar(255) = null,
  15788.     @fk_table_catalog    varchar(255) = null
  15789.     )
  15790. as
  15791.     declare @ret int
  15792.     
  15793.     SET NOCOUNT ON
  15794.  
  15795.     create table #spfkeysrowset1
  15796.         (
  15797.         PK_TABLE_CATALOG    sysname not null,
  15798.         PK_TABLE_SCHEMA        sysname not null,
  15799.         PK_TABLE_NAME         sysname not null,
  15800.         PK_COLUMN_NAME         sysname not null,
  15801.         PK_COLUMN_GUID        binary(16) null,
  15802.         PK_COLUMN_PROPID    int null,
  15803.         FK_TABLE_CATALOG    sysname not null,
  15804.         FK_TABLE_SCHEMA        sysname not null,
  15805.         FK_TABLE_NAME         sysname not null,
  15806.         FK_COLUMN_NAME         sysname not null,
  15807.         FK_COLUMN_GUID        binary(16) null,
  15808.         FK_COLUMN_PROPID    int null,
  15809.         ORDINAL                int not null,
  15810.         UPDATE_RULE            sysname not null,
  15811.         DELETE_RULE         sysname not null,
  15812.         PK_NAME                sysname not null,
  15813.         FK_NAME                sysname not null,
  15814.         DEFERRABILITY        smallint not null        
  15815.         )
  15816.     BEGIN
  15817.     insert into #spfkeysrowset1
  15818.     select
  15819.         db_name(r.rkeydbid),
  15820.         user_name(o1.uid),
  15821.         o1.name,
  15822.         c1.name,
  15823.         convert(binary(16),null),
  15824.         convert(int,null),
  15825.         db_name(r.fkeydbid),
  15826.         user_name(o2.uid),
  15827.         o2.name,
  15828.         c2.name,
  15829.         convert(binary(16),null),
  15830.         convert(int,null),
  15831.         convert(int,1),
  15832.         'NO ACTION',
  15833.         'NO ACTION',
  15834.         i.name,
  15835.         object_name(r.constid),
  15836.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15837.     from
  15838.         sysobjects o1, sysobjects o2,
  15839.         syscolumns c1, syscolumns c2,
  15840.         sysreferences r, sysindexes i, sysusers u
  15841.     where    
  15842.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15843.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15844.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15845.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15846.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15847.     and    o1.id = r.rkeyid
  15848.     and    o1.id = c1.id
  15849.     and    c1.colid = r.rkey1
  15850.     and r.fkeyid = o2.id
  15851.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15852.     and    o2.id = c2.id    
  15853.     and    c2.colid = r.fkey1
  15854.     and i.id = r.rkeyid
  15855.     and i.indid = r.rkeyindid
  15856.     and u.uid = user_id()
  15857.     and ( suser_id() = 1     /* User is the System Administrator */
  15858.     or (
  15859.         o1.uid = user_id()     /* User created the object */
  15860.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15861.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15862.             from sysprotects p
  15863.             where p.id = o1.id
  15864.             /* get rows for public,current user,user's group */
  15865.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15866.             /* check for SELECT,EXECUTE privilege */
  15867.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15868.             ) = 1    /* final magic...compare Grants      */
  15869.         )
  15870.     and (
  15871.         o2.uid = user_id()     /* User created the object */
  15872.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15873.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15874.             from sysprotects p
  15875.             where p.id = o2.id
  15876.             /* get rows for public,current user,user's group */
  15877.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15878.             /* check for SELECT,EXECUTE privilege */
  15879.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15880.             ) = 1    /* final magic...compare Grants      */
  15881.         )
  15882.     )    
  15883.     union all
  15884.     select
  15885.         db_name(r.rkeydbid),
  15886.         user_name(o1.uid),
  15887.         o1.name,
  15888.         c1.name,
  15889.         convert(binary(16),null),
  15890.         convert(int,null),
  15891.         db_name(r.fkeydbid),
  15892.         user_name(o2.uid),
  15893.         o2.name,
  15894.         c2.name,
  15895.         convert(binary(16),null),
  15896.         convert(int,null),
  15897.         convert(int,2),
  15898.         'NO ACTION',
  15899.         'NO ACTION',
  15900.         i.name,
  15901.         object_name(r.constid),
  15902.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15903.     from
  15904.         sysobjects o1, sysobjects o2,
  15905.         syscolumns c1, syscolumns c2,
  15906.         sysreferences r, sysindexes i, sysusers u
  15907.     where    
  15908.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15909.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15910.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15911.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15912.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15913.     and    o1.id = r.rkeyid
  15914.     and    r.keycnt >= 2
  15915.     and    o1.id = c1.id
  15916.     and    c1.colid = r.rkey2
  15917.     and r.fkeyid = o2.id
  15918.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15919.     and    o2.id = c2.id    
  15920.     and    c2.colid = r.fkey2
  15921.     and i.id = r.rkeyid
  15922.     and i.indid = r.rkeyindid
  15923.     and u.uid = user_id()
  15924.     and ( suser_id() = 1     /* User is the System Administrator */
  15925.     or (
  15926.         o1.uid = user_id()     /* User created the object */
  15927.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15928.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15929.             from sysprotects p
  15930.             where p.id = o1.id
  15931.             /* get rows for public,current user,user's group */
  15932.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15933.             /* check for SELECT,EXECUTE privilege */
  15934.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15935.             ) = 1    /* final magic...compare Grants      */
  15936.         )
  15937.     and (
  15938.         o2.uid = user_id()     /* User created the object */
  15939.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15940.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15941.             from sysprotects p
  15942.             where p.id = o2.id
  15943.             /* get rows for public,current user,user's group */
  15944.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15945.             /* check for SELECT,EXECUTE privilege */
  15946.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15947.             ) = 1    /* final magic...compare Grants      */
  15948.         )
  15949.     )    
  15950.     union all
  15951.     select
  15952.         db_name(r.rkeydbid),
  15953.         user_name(o1.uid),
  15954.         o1.name,
  15955.         c1.name,
  15956.         convert(binary(16),null),
  15957.         convert(int,null),
  15958.         db_name(r.fkeydbid),
  15959.         user_name(o2.uid),
  15960.         o2.name,
  15961.         c2.name,
  15962.         convert(binary(16),null),
  15963.         convert(int,null),
  15964.         convert(int,3),
  15965.         'NO ACTION',
  15966.         'NO ACTION',
  15967.         i.name,
  15968.         object_name(r.constid),
  15969.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15970.     from
  15971.         sysobjects o1, sysobjects o2,
  15972.         syscolumns c1, syscolumns c2,
  15973.         sysreferences r, sysindexes i, sysusers u
  15974.     where    
  15975.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15976.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15977.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15978.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15979.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15980.     and    o1.id = r.rkeyid
  15981.     and    r.keycnt >= 3
  15982.     and    o1.id = c1.id
  15983.     and    c1.colid = r.rkey3
  15984.     and r.fkeyid = o2.id
  15985.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15986.     and    o2.id = c2.id    
  15987.     and    c2.colid = r.fkey3
  15988.     and i.id = r.rkeyid
  15989.     and i.indid = r.rkeyindid
  15990.     and u.uid = user_id()
  15991.     and ( suser_id() = 1     /* User is the System Administrator */
  15992.     or (
  15993.         o1.uid = user_id()     /* User created the object */
  15994.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15995.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15996.             from sysprotects p
  15997.             where p.id = o1.id
  15998.             /* get rows for public,current user,user's group */
  15999.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16000.             /* check for SELECT,EXECUTE privilege */
  16001.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16002.             ) = 1    /* final magic...compare Grants      */
  16003.         )
  16004.     and (
  16005.         o2.uid = user_id()     /* User created the object */
  16006.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16007.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16008.             from sysprotects p
  16009.             where p.id = o2.id
  16010.             /* get rows for public,current user,user's group */
  16011.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16012.             /* check for SELECT,EXECUTE privilege */
  16013.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16014.             ) = 1    /* final magic...compare Grants      */
  16015.         )
  16016.     )    
  16017.     union all
  16018.     select
  16019.         db_name(r.rkeydbid),
  16020.         user_name(o1.uid),
  16021.         o1.name,
  16022.         c1.name,
  16023.         convert(binary(16),null),
  16024.         convert(int,null),
  16025.         db_name(r.fkeydbid),
  16026.         user_name(o2.uid),
  16027.         o2.name,
  16028.         c2.name,
  16029.         convert(binary(16),null),
  16030.         convert(int,null),
  16031.         convert(int,4),
  16032.         'NO ACTION',
  16033.         'NO ACTION',
  16034.         i.name,
  16035.         object_name(r.constid),
  16036.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16037.     from
  16038.         sysobjects o1, sysobjects o2,
  16039.         syscolumns c1, syscolumns c2,
  16040.         sysreferences r, sysindexes i, sysusers u
  16041.     where    
  16042.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16043.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16044.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16045.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16046.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16047.     and    o1.id = r.rkeyid
  16048.     and    r.keycnt >= 4
  16049.     and    o1.id = c1.id
  16050.     and    c1.colid = r.rkey4
  16051.     and r.fkeyid = o2.id
  16052.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16053.     and    o2.id = c2.id    
  16054.     and    c2.colid = r.fkey4
  16055.     and i.id = r.rkeyid
  16056.     and i.indid = r.rkeyindid
  16057.     and u.uid = user_id()
  16058.     and ( suser_id() = 1     /* User is the System Administrator */
  16059.     or (
  16060.         o1.uid = user_id()     /* User created the object */
  16061.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16062.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16063.             from sysprotects p
  16064.             where p.id = o1.id
  16065.             /* get rows for public,current user,user's group */
  16066.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16067.             /* check for SELECT,EXECUTE privilege */
  16068.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16069.             ) = 1    /* final magic...compare Grants      */
  16070.         )
  16071.     and (
  16072.         o2.uid = user_id()     /* User created the object */
  16073.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16074.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16075.             from sysprotects p
  16076.             where p.id = o2.id
  16077.             /* get rows for public,current user,user's group */
  16078.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16079.             /* check for SELECT,EXECUTE privilege */
  16080.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16081.             ) = 1    /* final magic...compare Grants      */
  16082.         )
  16083.     )    
  16084.     union all
  16085.     select
  16086.         db_name(r.rkeydbid),
  16087.         user_name(o1.uid),
  16088.         o1.name,
  16089.         c1.name,
  16090.         convert(binary(16),null),
  16091.         convert(int,null),
  16092.         db_name(r.fkeydbid),
  16093.         user_name(o2.uid),
  16094.         o2.name,
  16095.         c2.name,
  16096.         convert(binary(16),null),
  16097.         convert(int,null),
  16098.         convert(int,5),
  16099.         'NO ACTION',
  16100.         'NO ACTION',
  16101.         i.name,
  16102.         object_name(r.constid),
  16103.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16104.     from
  16105.         sysobjects o1, sysobjects o2,
  16106.         syscolumns c1, syscolumns c2,
  16107.         sysreferences r, sysindexes i, sysusers u
  16108.     where    
  16109.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16110.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16111.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16112.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16113.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16114.     and    o1.id = r.rkeyid
  16115.     and    r.keycnt >= 5
  16116.     and    o1.id = c1.id
  16117.     and    c1.colid = r.rkey5
  16118.     and r.fkeyid = o2.id
  16119.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16120.     and    o2.id = c2.id    
  16121.     and    c2.colid = r.fkey5
  16122.     and i.id = r.rkeyid
  16123.     and i.indid = r.rkeyindid
  16124.     and u.uid = user_id()
  16125.     and ( suser_id() = 1     /* User is the System Administrator */
  16126.     or (
  16127.         o1.uid = user_id()     /* User created the object */
  16128.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16129.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16130.             from sysprotects p
  16131.             where p.id = o1.id
  16132.             /* get rows for public,current user,user's group */
  16133.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16134.             /* check for SELECT,EXECUTE privilege */
  16135.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16136.             ) = 1    /* final magic...compare Grants      */
  16137.         )
  16138.     and (
  16139.         o2.uid = user_id()     /* User created the object */
  16140.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16141.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16142.             from sysprotects p
  16143.             where p.id = o2.id
  16144.             /* get rows for public,current user,user's group */
  16145.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16146.             /* check for SELECT,EXECUTE privilege */
  16147.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16148.             ) = 1    /* final magic...compare Grants      */
  16149.         )
  16150.     )    
  16151.     union all
  16152.     select
  16153.         db_name(r.rkeydbid),
  16154.         user_name(o1.uid),
  16155.         o1.name,
  16156.         c1.name,
  16157.         convert(binary(16),null),
  16158.         convert(int,null),
  16159.         db_name(r.fkeydbid),
  16160.         user_name(o2.uid),
  16161.         o2.name,
  16162.         c2.name,
  16163.         convert(binary(16),null),
  16164.         convert(int,null),
  16165.         convert(int,6),
  16166.         'NO ACTION',
  16167.         'NO ACTION',
  16168.         i.name,
  16169.         object_name(r.constid),
  16170.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16171.     from
  16172.         sysobjects o1, sysobjects o2,
  16173.         syscolumns c1, syscolumns c2,
  16174.         sysreferences r, sysindexes i, sysusers u
  16175.     where    
  16176.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16177.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16178.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16179.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16180.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16181.     and    o1.id = r.rkeyid
  16182.     and    r.keycnt >= 6
  16183.     and    o1.id = c1.id
  16184.     and    c1.colid = r.rkey6
  16185.     and r.fkeyid = o2.id
  16186.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16187.     and    o2.id = c2.id    
  16188.     and    c2.colid = r.fkey6
  16189.     and i.id = r.rkeyid
  16190.     and i.indid = r.rkeyindid
  16191.     and u.uid = user_id()
  16192.     and ( suser_id() = 1     /* User is the System Administrator */
  16193.     or (
  16194.         o1.uid = user_id()     /* User created the object */
  16195.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16196.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16197.             from sysprotects p
  16198.             where p.id = o1.id
  16199.             /* get rows for public,current user,user's group */
  16200.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16201.             /* check for SELECT,EXECUTE privilege */
  16202.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16203.             ) = 1    /* final magic...compare Grants      */
  16204.         )
  16205.     and (
  16206.         o2.uid = user_id()     /* User created the object */
  16207.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16208.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16209.             from sysprotects p
  16210.             where p.id = o2.id
  16211.             /* get rows for public,current user,user's group */
  16212.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16213.             /* check for SELECT,EXECUTE privilege */
  16214.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16215.             ) = 1    /* final magic...compare Grants      */
  16216.         )
  16217.     )    
  16218.     union all
  16219.     select
  16220.         db_name(r.rkeydbid),
  16221.         user_name(o1.uid),
  16222.         o1.name,
  16223.         c1.name,
  16224.         convert(binary(16),null),
  16225.         convert(int,null),
  16226.         db_name(r.fkeydbid),
  16227.         user_name(o2.uid),
  16228.         o2.name,
  16229.         c2.name,
  16230.         convert(binary(16),null),
  16231.         convert(int,null),
  16232.         convert(int,7),
  16233.         'NO ACTION',
  16234.         'NO ACTION',
  16235.         i.name,
  16236.         object_name(r.constid),
  16237.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16238.     from
  16239.         sysobjects o1, sysobjects o2,
  16240.         syscolumns c1, syscolumns c2,
  16241.         sysreferences r, sysindexes i, sysusers u
  16242.     where    
  16243.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16244.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16245.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16246.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16247.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16248.     and    o1.id = r.rkeyid
  16249.     and    r.keycnt >= 7
  16250.     and    o1.id = c1.id
  16251.     and    c1.colid = r.rkey7
  16252.     and r.fkeyid = o2.id
  16253.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16254.     and    o2.id = c2.id    
  16255.     and    c2.colid = r.fkey7
  16256.     and i.id = r.rkeyid
  16257.     and i.indid = r.rkeyindid
  16258.     and u.uid = user_id()
  16259.     and ( suser_id() = 1     /* User is the System Administrator */
  16260.     or (
  16261.         o1.uid = user_id()     /* User created the object */
  16262.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16263.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16264.             from sysprotects p
  16265.             where p.id = o1.id
  16266.             /* get rows for public,current user,user's group */
  16267.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16268.             /* check for SELECT,EXECUTE privilege */
  16269.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16270.             ) = 1    /* final magic...compare Grants      */
  16271.         )
  16272.     and (
  16273.         o2.uid = user_id()     /* User created the object */
  16274.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16275.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16276.             from sysprotects p
  16277.             where p.id = o2.id
  16278.             /* get rows for public,current user,user's group */
  16279.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16280.             /* check for SELECT,EXECUTE privilege */
  16281.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16282.             ) = 1    /* final magic...compare Grants      */
  16283.         )
  16284.     )    
  16285.     union all
  16286.     select
  16287.         db_name(r.rkeydbid),
  16288.         user_name(o1.uid),
  16289.         o1.name,
  16290.         c1.name,
  16291.         convert(binary(16),null),
  16292.         convert(int,null),
  16293.         db_name(r.fkeydbid),
  16294.         user_name(o2.uid),
  16295.         o2.name,
  16296.         c2.name,
  16297.         convert(binary(16),null),
  16298.         convert(int,null),
  16299.         convert(int,8),
  16300.         'NO ACTION',
  16301.         'NO ACTION',
  16302.         i.name,
  16303.         object_name(r.constid),
  16304.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16305.     from
  16306.         sysobjects o1, sysobjects o2,
  16307.         syscolumns c1, syscolumns c2,
  16308.         sysreferences r, sysindexes i, sysusers u
  16309.     where    
  16310.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16311.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16312.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16313.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16314.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16315.     and    o1.id = r.rkeyid
  16316.     and    r.keycnt >= 8
  16317.     and    o1.id = c1.id
  16318.     and    c1.colid = r.rkey8
  16319.     and r.fkeyid = o2.id
  16320.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16321.     and    o2.id = c2.id    
  16322.     and    c2.colid = r.fkey8
  16323.     and i.id = r.rkeyid
  16324.     and i.indid = r.rkeyindid
  16325.     and u.uid = user_id()
  16326.     and ( suser_id() = 1     /* User is the System Administrator */
  16327.     or (
  16328.         o1.uid = user_id()     /* User created the object */
  16329.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16330.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16331.             from sysprotects p
  16332.             where p.id = o1.id
  16333.             /* get rows for public,current user,user's group */
  16334.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16335.             /* check for SELECT,EXECUTE privilege */
  16336.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16337.             ) = 1    /* final magic...compare Grants      */
  16338.         )
  16339.     and (
  16340.         o2.uid = user_id()     /* User created the object */
  16341.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16342.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16343.             from sysprotects p
  16344.             where p.id = o2.id
  16345.             /* get rows for public,current user,user's group */
  16346.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16347.             /* check for SELECT,EXECUTE privilege */
  16348.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16349.             ) = 1    /* final magic...compare Grants      */
  16350.         )
  16351.     )    
  16352.     union all
  16353.     select
  16354.         db_name(r.rkeydbid),
  16355.         user_name(o1.uid),
  16356.         o1.name,
  16357.         c1.name,
  16358.         convert(binary(16),null),
  16359.         convert(int,null),
  16360.         db_name(r.fkeydbid),
  16361.         user_name(o2.uid),
  16362.         o2.name,
  16363.         c2.name,
  16364.         convert(binary(16),null),
  16365.         convert(int,null),
  16366.         convert(int,9),
  16367.         'NO ACTION',
  16368.         'NO ACTION',
  16369.         i.name,
  16370.         object_name(r.constid),
  16371.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16372.     from
  16373.         sysobjects o1, sysobjects o2,
  16374.         syscolumns c1, syscolumns c2,
  16375.         sysreferences r, sysindexes i, sysusers u
  16376.     where    
  16377.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16378.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16379.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16380.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16381.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16382.     and    o1.id = r.rkeyid
  16383.     and    r.keycnt >= 9
  16384.     and    o1.id = c1.id
  16385.     and    c1.colid = r.rkey9
  16386.     and r.fkeyid = o2.id
  16387.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16388.     and    o2.id = c2.id    
  16389.     and    c2.colid = r.fkey9
  16390.     and i.id = r.rkeyid
  16391.     and i.indid = r.rkeyindid
  16392.     and u.uid = user_id()
  16393.     and ( suser_id() = 1     /* User is the System Administrator */
  16394.     or (
  16395.         o1.uid = user_id()     /* User created the object */
  16396.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16397.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16398.             from sysprotects p
  16399.             where p.id = o1.id
  16400.             /* get rows for public,current user,user's group */
  16401.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16402.             /* check for SELECT,EXECUTE privilege */
  16403.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16404.             ) = 1    /* final magic...compare Grants      */
  16405.         )
  16406.     and (
  16407.         o2.uid = user_id()     /* User created the object */
  16408.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16409.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16410.             from sysprotects p
  16411.             where p.id = o2.id
  16412.             /* get rows for public,current user,user's group */
  16413.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16414.             /* check for SELECT,EXECUTE privilege */
  16415.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16416.             ) = 1    /* final magic...compare Grants      */
  16417.         )
  16418.     )    
  16419.     union all
  16420.     select
  16421.         db_name(r.rkeydbid),
  16422.         user_name(o1.uid),
  16423.         o1.name,
  16424.         c1.name,
  16425.         convert(binary(16),null),
  16426.         convert(int,null),
  16427.         db_name(r.fkeydbid),
  16428.         user_name(o2.uid),
  16429.         o2.name,
  16430.         c2.name,
  16431.         convert(binary(16),null),
  16432.         convert(int,null),
  16433.         convert(int,10),
  16434.         'NO ACTION',
  16435.         'NO ACTION',
  16436.         i.name,
  16437.         object_name(r.constid),
  16438.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16439.     from
  16440.         sysobjects o1, sysobjects o2,
  16441.         syscolumns c1, syscolumns c2,
  16442.         sysreferences r, sysindexes i, sysusers u
  16443.     where    
  16444.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16445.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16446.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16447.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16448.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16449.     and    o1.id = r.rkeyid
  16450.     and    r.keycnt >= 10
  16451.     and    o1.id = c1.id
  16452.     and    c1.colid = r.rkey10
  16453.     and r.fkeyid = o2.id
  16454.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16455.     and    o2.id = c2.id    
  16456.     and    c2.colid = r.fkey10
  16457.     and i.id = r.rkeyid
  16458.     and i.indid = r.rkeyindid
  16459.     and u.uid = user_id()
  16460.     and ( suser_id() = 1     /* User is the System Administrator */
  16461.     or (
  16462.         o1.uid = user_id()     /* User created the object */
  16463.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16464.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16465.             from sysprotects p
  16466.             where p.id = o1.id
  16467.             /* get rows for public,current user,user's group */
  16468.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16469.             /* check for SELECT,EXECUTE privilege */
  16470.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16471.             ) = 1    /* final magic...compare Grants      */
  16472.         )
  16473.     and (
  16474.         o2.uid = user_id()     /* User created the object */
  16475.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16476.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16477.             from sysprotects p
  16478.             where p.id = o2.id
  16479.             /* get rows for public,current user,user's group */
  16480.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16481.             /* check for SELECT,EXECUTE privilege */
  16482.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16483.             ) = 1    /* final magic...compare Grants      */
  16484.         )
  16485.     )    
  16486.     union all
  16487.     select
  16488.         db_name(r.rkeydbid),
  16489.         user_name(o1.uid),
  16490.         o1.name,
  16491.         c1.name,
  16492.         convert(binary(16),null),
  16493.         convert(int,null),
  16494.         db_name(r.fkeydbid),
  16495.         user_name(o2.uid),
  16496.         o2.name,
  16497.         c2.name,
  16498.         convert(binary(16),null),
  16499.         convert(int,null),
  16500.         convert(int,11),
  16501.         'NO ACTION',
  16502.         'NO ACTION',
  16503.         i.name,
  16504.         object_name(r.constid),
  16505.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16506.     from
  16507.         sysobjects o1, sysobjects o2,
  16508.         syscolumns c1, syscolumns c2,
  16509.         sysreferences r, sysindexes i, sysusers u
  16510.     where    
  16511.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16512.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16513.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16514.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16515.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16516.     and    o1.id = r.rkeyid
  16517.     and    r.keycnt >= 11
  16518.     and    o1.id = c1.id
  16519.     and    c1.colid = r.rkey11
  16520.     and r.fkeyid = o2.id
  16521.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16522.     and    o2.id = c2.id    
  16523.     and    c2.colid = r.fkey11
  16524.     and i.id = r.rkeyid
  16525.     and i.indid = r.rkeyindid
  16526.     and u.uid = user_id()
  16527.     and ( suser_id() = 1     /* User is the System Administrator */
  16528.     or (
  16529.         o1.uid = user_id()     /* User created the object */
  16530.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16531.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16532.             from sysprotects p
  16533.             where p.id = o1.id
  16534.             /* get rows for public,current user,user's group */
  16535.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16536.             /* check for SELECT,EXECUTE privilege */
  16537.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16538.             ) = 1    /* final magic...compare Grants      */
  16539.         )
  16540.     and (
  16541.         o2.uid = user_id()     /* User created the object */
  16542.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16543.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16544.             from sysprotects p
  16545.             where p.id = o2.id
  16546.             /* get rows for public,current user,user's group */
  16547.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16548.             /* check for SELECT,EXECUTE privilege */
  16549.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16550.             ) = 1    /* final magic...compare Grants      */
  16551.         )
  16552.     )    
  16553.     union all
  16554.     select
  16555.         db_name(r.rkeydbid),
  16556.         user_name(o1.uid),
  16557.         o1.name,
  16558.         c1.name,
  16559.         convert(binary(16),null),
  16560.         convert(int,null),
  16561.         db_name(r.fkeydbid),
  16562.         user_name(o2.uid),
  16563.         o2.name,
  16564.         c2.name,
  16565.         convert(binary(16),null),
  16566.         convert(int,null),
  16567.         convert(int,12),
  16568.         'NO ACTION',
  16569.         'NO ACTION',
  16570.         i.name,
  16571.         object_name(r.constid),
  16572.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16573.     from
  16574.         sysobjects o1, sysobjects o2,
  16575.         syscolumns c1, syscolumns c2,
  16576.         sysreferences r, sysindexes i, sysusers u
  16577.     where    
  16578.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16579.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16580.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16581.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16582.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16583.     and    o1.id = r.rkeyid
  16584.     and    r.keycnt >= 12
  16585.     and    o1.id = c1.id
  16586.     and    c1.colid = r.rkey12
  16587.     and r.fkeyid = o2.id
  16588.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16589.     and    o2.id = c2.id    
  16590.     and    c2.colid = r.fkey12
  16591.     and i.id = r.rkeyid
  16592.     and i.indid = r.rkeyindid
  16593.     and u.uid = user_id()
  16594.     and ( suser_id() = 1     /* User is the System Administrator */
  16595.     or (
  16596.         o1.uid = user_id()     /* User created the object */
  16597.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16598.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16599.             from sysprotects p
  16600.             where p.id = o1.id
  16601.             /* get rows for public,current user,user's group */
  16602.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16603.             /* check for SELECT,EXECUTE privilege */
  16604.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16605.             ) = 1    /* final magic...compare Grants      */
  16606.         )
  16607.     and (
  16608.         o2.uid = user_id()     /* User created the object */
  16609.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16610.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16611.             from sysprotects p
  16612.             where p.id = o2.id
  16613.             /* get rows for public,current user,user's group */
  16614.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16615.             /* check for SELECT,EXECUTE privilege */
  16616.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16617.             ) = 1    /* final magic...compare Grants      */
  16618.         )
  16619.     )    
  16620.     union all
  16621.     select
  16622.         db_name(r.rkeydbid),
  16623.         user_name(o1.uid),
  16624.         o1.name,
  16625.         c1.name,
  16626.         convert(binary(16),null),
  16627.         convert(int,null),
  16628.         db_name(r.fkeydbid),
  16629.         user_name(o2.uid),
  16630.         o2.name,
  16631.         c2.name,
  16632.         convert(binary(16),null),
  16633.         convert(int,null),
  16634.         convert(int,13),
  16635.         'NO ACTION',
  16636.         'NO ACTION',
  16637.         i.name,
  16638.         object_name(r.constid),
  16639.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16640.     from
  16641.         sysobjects o1, sysobjects o2,
  16642.         syscolumns c1, syscolumns c2,
  16643.         sysreferences r, sysindexes i, sysusers u
  16644.     where    
  16645.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16646.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16647.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16648.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16649.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16650.     and    o1.id = r.rkeyid
  16651.     and    r.keycnt >= 13
  16652.     and    o1.id = c1.id
  16653.     and    c1.colid = r.rkey13
  16654.     and r.fkeyid = o2.id
  16655.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16656.     and    o2.id = c2.id    
  16657.     and    c2.colid = r.fkey13
  16658.     and i.id = r.rkeyid
  16659.     and i.indid = r.rkeyindid
  16660.     and u.uid = user_id()
  16661.     and ( suser_id() = 1     /* User is the System Administrator */
  16662.     or (
  16663.         o1.uid = user_id()     /* User created the object */
  16664.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16665.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16666.             from sysprotects p
  16667.             where p.id = o1.id
  16668.             /* get rows for public,current user,user's group */
  16669.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16670.             /* check for SELECT,EXECUTE privilege */
  16671.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16672.             ) = 1    /* final magic...compare Grants      */
  16673.         )
  16674.     and (
  16675.         o2.uid = user_id()     /* User created the object */
  16676.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16677.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16678.             from sysprotects p
  16679.             where p.id = o2.id
  16680.             /* get rows for public,current user,user's group */
  16681.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16682.             /* check for SELECT,EXECUTE privilege */
  16683.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16684.             ) = 1    /* final magic...compare Grants      */
  16685.         )
  16686.     )    
  16687.     union all
  16688.     select
  16689.         db_name(r.rkeydbid),
  16690.         user_name(o1.uid),
  16691.         o1.name,
  16692.         c1.name,
  16693.         convert(binary(16),null),
  16694.         convert(int,null),
  16695.         db_name(r.fkeydbid),
  16696.         user_name(o2.uid),
  16697.         o2.name,
  16698.         c2.name,
  16699.         convert(binary(16),null),
  16700.         convert(int,null),
  16701.         convert(int,14),
  16702.         'NO ACTION',
  16703.         'NO ACTION',
  16704.         i.name,
  16705.         object_name(r.constid),
  16706.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16707.     from
  16708.         sysobjects o1, sysobjects o2,
  16709.         syscolumns c1, syscolumns c2,
  16710.         sysreferences r, sysindexes i, sysusers u
  16711.     where    
  16712.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16713.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16714.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16715.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16716.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16717.     and    o1.id = r.rkeyid
  16718.     and    r.keycnt >= 14
  16719.     and    o1.id = c1.id
  16720.     and    c1.colid = r.rkey14
  16721.     and r.fkeyid = o2.id
  16722.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16723.     and    o2.id = c2.id    
  16724.     and    c2.colid = r.fkey14
  16725.     and i.id = r.rkeyid
  16726.     and i.indid = r.rkeyindid
  16727.     and u.uid = user_id()
  16728.     and ( suser_id() = 1     /* User is the System Administrator */
  16729.     or (
  16730.         o1.uid = user_id()     /* User created the object */
  16731.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16732.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16733.             from sysprotects p
  16734.             where p.id = o1.id
  16735.             /* get rows for public,current user,user's group */
  16736.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16737.             /* check for SELECT,EXECUTE privilege */
  16738.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16739.             ) = 1    /* final magic...compare Grants      */
  16740.         )
  16741.     and (
  16742.         o2.uid = user_id()     /* User created the object */
  16743.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16744.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16745.             from sysprotects p
  16746.             where p.id = o2.id
  16747.             /* get rows for public,current user,user's group */
  16748.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16749.             /* check for SELECT,EXECUTE privilege */
  16750.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16751.             ) = 1    /* final magic...compare Grants      */
  16752.         )
  16753.     )    
  16754.     union all
  16755.     select
  16756.         db_name(r.rkeydbid),
  16757.         user_name(o1.uid),
  16758.         o1.name,
  16759.         c1.name,
  16760.         convert(binary(16),null),
  16761.         convert(int,null),
  16762.         db_name(r.fkeydbid),
  16763.         user_name(o2.uid),
  16764.         o2.name,
  16765.         c2.name,
  16766.         convert(binary(16),null),
  16767.         convert(int,null),
  16768.         convert(int,15),
  16769.         'NO ACTION',
  16770.         'NO ACTION',
  16771.         i.name,
  16772.         object_name(r.constid),
  16773.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16774.     from
  16775.         sysobjects o1, sysobjects o2,
  16776.         syscolumns c1, syscolumns c2,
  16777.         sysreferences r, sysindexes i, sysusers u
  16778.     where    
  16779.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16780.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16781.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16782.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16783.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16784.     and    o1.id = r.rkeyid
  16785.     and    r.keycnt >= 15
  16786.     and    o1.id = c1.id
  16787.     and    c1.colid = r.rkey15
  16788.     and r.fkeyid = o2.id
  16789.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16790.     and    o2.id = c2.id    
  16791.     and    c2.colid = r.fkey15
  16792.     and i.id = r.rkeyid
  16793.     and i.indid = r.rkeyindid
  16794.     and u.uid = user_id()
  16795.     and ( suser_id() = 1     /* User is the System Administrator */
  16796.     or (
  16797.         o1.uid = user_id()     /* User created the object */
  16798.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16799.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16800.             from sysprotects p
  16801.             where p.id = o1.id
  16802.             /* get rows for public,current user,user's group */
  16803.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16804.             /* check for SELECT,EXECUTE privilege */
  16805.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16806.             ) = 1    /* final magic...compare Grants      */
  16807.         )
  16808.     and (
  16809.         o2.uid = user_id()     /* User created the object */
  16810.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16811.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16812.             from sysprotects p
  16813.             where p.id = o2.id
  16814.             /* get rows for public,current user,user's group */
  16815.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16816.             /* check for SELECT,EXECUTE privilege */
  16817.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16818.             ) = 1    /* final magic...compare Grants      */
  16819.         )
  16820.     )    
  16821.     union all
  16822.     select
  16823.         db_name(r.rkeydbid),
  16824.         user_name(o1.uid),
  16825.         o1.name,
  16826.         c1.name,
  16827.         convert(binary(16),null),
  16828.         convert(int,null),
  16829.         db_name(r.fkeydbid),
  16830.         user_name(o2.uid),
  16831.         o2.name,
  16832.         c2.name,
  16833.         convert(binary(16),null),
  16834.         convert(int,null),
  16835.         convert(int,16),
  16836.         'NO ACTION',
  16837.         'NO ACTION',
  16838.         i.name,
  16839.         object_name(r.constid),
  16840.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16841.     from
  16842.         sysobjects o1, sysobjects o2,
  16843.         syscolumns c1, syscolumns c2,
  16844.         sysreferences r, sysindexes i, sysusers u
  16845.     where    
  16846.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16847.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16848.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16849.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16850.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16851.     and    o1.id = r.rkeyid
  16852.     and    o1.id = c1.id
  16853.     and    r.keycnt >= 16
  16854.     and    c1.colid = r.rkey16
  16855.     and r.fkeyid = o2.id
  16856.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16857.     and    o2.id = c2.id    
  16858.     and    c2.colid = r.fkey16
  16859.     and i.id = r.rkeyid
  16860.     and i.indid = r.rkeyindid
  16861.     and u.uid = user_id()
  16862.     and ( suser_id() = 1     /* User is the System Administrator */
  16863.     or (
  16864.         o1.uid = user_id()     /* User created the object */
  16865.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16866.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16867.             from sysprotects p
  16868.             where p.id = o1.id
  16869.             /* get rows for public,current user,user's group */
  16870.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16871.             /* check for SELECT,EXECUTE privilege */
  16872.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16873.             ) = 1    /* final magic...compare Grants      */
  16874.         )
  16875.     and (
  16876.         o2.uid = user_id()     /* User created the object */
  16877.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16878.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16879.             from sysprotects p
  16880.             where p.id = o2.id
  16881.             /* get rows for public,current user,user's group */
  16882.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16883.             /* check for SELECT,EXECUTE privilege */
  16884.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16885.             ) = 1    /* final magic...compare Grants      */
  16886.         )
  16887.     )
  16888.     order by 8,9,2,3,13
  16889.     END
  16890.  
  16891.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  16892.         @scrollopt output, @ccopt output, @rows output
  16893.  
  16894.     drop table #spfkeysrowset1
  16895.        return isnull(@ret,0)
  16896. go
  16897. dump tran master with no_log
  16898. go
  16899. CREATE PROCEDURE sp_foreign_keys_rowset;3
  16900. as
  16901.     select
  16902.         PK_TABLE_CATALOG    = convert(sysname,' '),
  16903.         PK_TABLE_SCHEMA        = convert(sysname,' '),
  16904.         PK_TABLE_NAME         = convert(sysname,' '),
  16905.         PK_COLUMN_NAME         = convert(sysname,' '),
  16906.         PK_COLUMN_GUID        = convert(binary(16),null),
  16907.         PK_COLUMN_PROPID    = convert(int,null),
  16908.         FK_TABLE_CATALOG    = convert(sysname,' '),
  16909.         FK_TABLE_SCHEMA        = convert(sysname,' '),
  16910.         FK_TABLE_NAME         = convert(sysname,' '),
  16911.         FK_COLUMN_NAME         = convert(sysname,' '),
  16912.         FK_COLUMN_GUID        = convert(binary(16),null),
  16913.         FK_COLUMN_PROPID    = convert(int,null),
  16914.         ORDINAL                = convert(int,1),
  16915.         UPDATE_RULE            = 'NO ACTION',
  16916.         DELETE_RULE         = 'NO ACTION',
  16917.         PK_NAME                = convert(sysname, ' '),
  16918.         FK_NAME                = convert(sysname, ' '),
  16919.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16920.     where    1=0
  16921. go
  16922. dump tran master with no_log
  16923. go
  16924.  
  16925.  
  16926. if (charindex('7.00', @@version) = 0 and
  16927.     charindex('8.00', @@version) = 0)
  16928. begin
  16929.     print ''
  16930.     print ''
  16931.     print 'Warning:'
  16932.     print 'you are installing the stored procedures '
  16933.     print 'on a pre 8.0 SQL Server.'
  16934.     print 'Ignore the following errors.'
  16935. end
  16936. else
  16937.     drop proc sp_foreign_keys_rowset
  16938. go
  16939.  
  16940.  
  16941. /*    Procedure for 8.0 server */
  16942. CREATE PROCEDURE sp_foreign_keys_rowset
  16943.     (
  16944.     @pk_table_name        sysname,
  16945.     @pk_table_schema    sysname = null,
  16946.     @fk_table_name        sysname = null,
  16947.     @fk_table_schema    sysname = null,
  16948.     @fk_table_catalog    sysname = null
  16949.     )
  16950. as
  16951.     select
  16952.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16953.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16954.         PK_TABLE_NAME         = o1.name,
  16955.         PK_COLUMN_NAME         = c1.name,
  16956.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16957.         PK_COLUMN_PROPID    = convert(int,null),
  16958.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16959.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16960.         FK_TABLE_NAME         = o2.name,
  16961.         FK_COLUMN_NAME         = c2.name,
  16962.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16963.         FK_COLUMN_PROPID    = convert(int,null),
  16964.         ORDINAL                = convert(int,1),
  16965.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  16966.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  16967.         PK_NAME                = i.name,
  16968.         FK_NAME                = object_name(r.constid),
  16969.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16970.     from
  16971.         sysobjects o1, sysobjects o2,
  16972.         syscolumns c1, syscolumns c2,
  16973.         sysreferences r, sysindexes i
  16974.     where    
  16975.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16976.     and    o1.name = @pk_table_name
  16977.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16978.     and    o1.id = r.rkeyid
  16979.     and    o1.id = c1.id
  16980.     and    c1.colid = r.rkey1
  16981.     and r.fkeyid = o2.id
  16982.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16983.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16984.     and    o2.id = c2.id    
  16985.     and    c2.colid = r.fkey1
  16986.     and permissions(o1.id) <> 0
  16987.     and permissions(o2.id) <> 0
  16988.     and i.id = r.rkeyid
  16989.     and i.indid = r.rkeyindid
  16990.     union all
  16991.     select
  16992.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16993.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16994.         PK_TABLE_NAME         = o1.name,
  16995.         PK_COLUMN_NAME         = c1.name,
  16996.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16997.         PK_COLUMN_PROPID    = convert(int,null),
  16998.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16999.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17000.         FK_TABLE_NAME         = o2.name,
  17001.         FK_COLUMN_NAME         = c2.name,
  17002.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17003.         FK_COLUMN_PROPID    = convert(int,null),
  17004.         ORDINAL                = convert(int,2),
  17005.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17006.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17007.         PK_NAME                = i.name,
  17008.         FK_NAME                = object_name(r.constid),
  17009.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17010.     from
  17011.         sysobjects o1, sysobjects o2,
  17012.         syscolumns c1, syscolumns c2,
  17013.         sysreferences r, sysindexes i
  17014.     where    
  17015.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17016.     and    o1.name = @pk_table_name
  17017.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17018.     and    o1.id = r.rkeyid
  17019.     and    o1.id = c1.id
  17020.     and    c1.colid = r.rkey2
  17021.     and    r.keycnt >= 2
  17022.     and r.fkeyid = o2.id
  17023.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17024.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17025.     and    o2.id = c2.id    
  17026.     and    c2.colid = r.fkey2
  17027.     and permissions(o1.id) <> 0
  17028.     and permissions(o2.id) <> 0
  17029.     and i.id = r.rkeyid
  17030.     and i.indid = r.rkeyindid
  17031.     union all
  17032.     select
  17033.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17034.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17035.         PK_TABLE_NAME         = o1.name,
  17036.         PK_COLUMN_NAME         = c1.name,
  17037.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17038.         PK_COLUMN_PROPID    = convert(int,null),
  17039.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17040.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17041.         FK_TABLE_NAME         = o2.name,
  17042.         FK_COLUMN_NAME         = c2.name,
  17043.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17044.         FK_COLUMN_PROPID    = convert(int,null),
  17045.         ORDINAL                = convert(int,3),
  17046.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17047.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17048.         PK_NAME                = i.name,
  17049.         FK_NAME                = object_name(r.constid),
  17050.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17051.     from
  17052.         sysobjects o1, sysobjects o2,
  17053.         syscolumns c1, syscolumns c2,
  17054.         sysreferences r, sysindexes i
  17055.     where    
  17056.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17057.     and    o1.name = @pk_table_name
  17058.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17059.     and    o1.id = r.rkeyid
  17060.     and    o1.id = c1.id
  17061.     and    c1.colid = r.rkey3
  17062.     and    r.keycnt >= 3
  17063.     and r.fkeyid = o2.id
  17064.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17065.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17066.     and    o2.id = c2.id    
  17067.     and    c2.colid = r.fkey3
  17068.     and permissions(o1.id) <> 0
  17069.     and permissions(o2.id) <> 0    
  17070.     and i.id = r.rkeyid
  17071.     and i.indid = r.rkeyindid
  17072.     union all
  17073.     select
  17074.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17075.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17076.         PK_TABLE_NAME         = o1.name,
  17077.         PK_COLUMN_NAME         = c1.name,
  17078.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17079.         PK_COLUMN_PROPID    = convert(int,null),
  17080.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17081.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17082.         FK_TABLE_NAME         = o2.name,
  17083.         FK_COLUMN_NAME         = c2.name,
  17084.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17085.         FK_COLUMN_PROPID    = convert(int,null),
  17086.         ORDINAL                = convert(int,4),
  17087.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17088.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17089.         PK_NAME                = i.name,
  17090.         FK_NAME                = object_name(r.constid),
  17091.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17092.     from
  17093.         sysobjects o1, sysobjects o2,
  17094.         syscolumns c1, syscolumns c2,
  17095.         sysreferences r, sysindexes i
  17096.     where    
  17097.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17098.     and    o1.name = @pk_table_name
  17099.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17100.     and    o1.id = r.rkeyid
  17101.     and    r.keycnt >= 4
  17102.     and    o1.id = c1.id
  17103.     and    c1.colid = r.rkey4
  17104.     and r.fkeyid = o2.id
  17105.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17106.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17107.     and    o2.id = c2.id    
  17108.     and    c2.colid = r.fkey4
  17109.     and permissions(o1.id) <> 0
  17110.     and permissions(o2.id) <> 0
  17111.     and i.id = r.rkeyid
  17112.     and i.indid = r.rkeyindid    
  17113.     union all
  17114.     select
  17115.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17116.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17117.         PK_TABLE_NAME         = o1.name,
  17118.         PK_COLUMN_NAME         = c1.name,
  17119.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17120.         PK_COLUMN_PROPID    = convert(int,null),
  17121.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17122.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17123.         FK_TABLE_NAME         = o2.name,
  17124.         FK_COLUMN_NAME         = c2.name,
  17125.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17126.         FK_COLUMN_PROPID    = convert(int,null),
  17127.         ORDINAL                = convert(int,5),
  17128.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17129.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17130.         PK_NAME                = i.name,
  17131.         FK_NAME                = object_name(r.constid),
  17132.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17133.     from
  17134.         sysobjects o1, sysobjects o2,
  17135.         syscolumns c1, syscolumns c2,
  17136.         sysreferences r, sysindexes i
  17137.     where    
  17138.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17139.     and    o1.name = @pk_table_name
  17140.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17141.     and    o1.id = r.rkeyid
  17142.     and    r.keycnt >= 5
  17143.     and    o1.id = c1.id
  17144.     and    c1.colid = r.rkey5
  17145.     and r.fkeyid = o2.id
  17146.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17147.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17148.     and    o2.id = c2.id    
  17149.     and    c2.colid = r.fkey5
  17150.     and permissions(o1.id) <> 0
  17151.     and permissions(o2.id) <> 0
  17152.     and i.id = r.rkeyid
  17153.     and i.indid = r.rkeyindid        
  17154.     union all
  17155.     select
  17156.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17157.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17158.         PK_TABLE_NAME         = o1.name,
  17159.         PK_COLUMN_NAME         = c1.name,
  17160.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17161.         PK_COLUMN_PROPID    = convert(int,null),
  17162.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17163.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17164.         FK_TABLE_NAME         = o2.name,
  17165.         FK_COLUMN_NAME         = c2.name,
  17166.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17167.         FK_COLUMN_PROPID    = convert(int,null),
  17168.         ORDINAL                = convert(int,6),
  17169.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17170.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17171.         PK_NAME                = i.name,
  17172.         FK_NAME                = object_name(r.constid),
  17173.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17174.     from
  17175.         sysobjects o1, sysobjects o2,
  17176.         syscolumns c1, syscolumns c2,
  17177.         sysreferences r, sysindexes i
  17178.     where    
  17179.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17180.     and    o1.name = @pk_table_name
  17181.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17182.     and    o1.id = r.rkeyid
  17183.     and    r.keycnt >= 6
  17184.     and    o1.id = c1.id
  17185.     and    c1.colid = r.rkey6
  17186.     and r.fkeyid = o2.id
  17187.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17188.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17189.     and    o2.id = c2.id    
  17190.     and    c2.colid = r.fkey6
  17191.     and permissions(o1.id) <> 0
  17192.     and permissions(o2.id) <> 0
  17193.     and i.id = r.rkeyid
  17194.     and i.indid = r.rkeyindid            
  17195.     union all
  17196.     select
  17197.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17198.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17199.         PK_TABLE_NAME         = o1.name,
  17200.         PK_COLUMN_NAME         = c1.name,
  17201.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17202.         PK_COLUMN_PROPID    = convert(int,null),
  17203.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17204.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17205.         FK_TABLE_NAME         = o2.name,
  17206.         FK_COLUMN_NAME         = c2.name,
  17207.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17208.         FK_COLUMN_PROPID    = convert(int,null),
  17209.         ORDINAL                = convert(int,7),
  17210.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17211.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17212.         PK_NAME                = i.name,
  17213.         FK_NAME                = object_name(r.constid),
  17214.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17215.     from
  17216.         sysobjects o1, sysobjects o2,
  17217.         syscolumns c1, syscolumns c2,
  17218.         sysreferences r, sysindexes i
  17219.     where    
  17220.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17221.     and    o1.name = @pk_table_name
  17222.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17223.     and    o1.id = r.rkeyid
  17224.     and    r.keycnt >= 7
  17225.     and    o1.id = c1.id
  17226.     and    c1.colid = r.rkey7
  17227.     and r.fkeyid = o2.id
  17228.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17229.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17230.     and    o2.id = c2.id    
  17231.     and    c2.colid = r.fkey7
  17232.     and permissions(o1.id) <> 0
  17233.     and permissions(o2.id) <> 0
  17234.     and i.id = r.rkeyid
  17235.     and i.indid = r.rkeyindid            
  17236.     union all
  17237.     select
  17238.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17239.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17240.         PK_TABLE_NAME         = o1.name,
  17241.         PK_COLUMN_NAME         = c1.name,
  17242.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17243.         PK_COLUMN_PROPID    = convert(int,null),
  17244.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17245.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17246.         FK_TABLE_NAME         = o2.name,
  17247.         FK_COLUMN_NAME         = c2.name,
  17248.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17249.         FK_COLUMN_PROPID    = convert(int,null),
  17250.         ORDINAL                = convert(int,8),
  17251.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17252.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17253.         PK_NAME                = i.name,
  17254.         FK_NAME                = object_name(r.constid),
  17255.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17256.     from
  17257.         sysobjects o1, sysobjects o2,
  17258.         syscolumns c1, syscolumns c2,
  17259.         sysreferences r, sysindexes i
  17260.     where    
  17261.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17262.     and    o1.name = @pk_table_name
  17263.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17264.     and    o1.id = r.rkeyid
  17265.     and    r.keycnt >= 8
  17266.     and    o1.id = c1.id
  17267.     and    c1.colid = r.rkey8
  17268.     and r.fkeyid = o2.id
  17269.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17270.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17271.     and    o2.id = c2.id    
  17272.     and    c2.colid = r.fkey8
  17273.     and permissions(o1.id) <> 0
  17274.     and permissions(o2.id) <> 0
  17275.     and i.id = r.rkeyid
  17276.     and i.indid = r.rkeyindid            
  17277.     union all
  17278.     select
  17279.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17280.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17281.         PK_TABLE_NAME         = o1.name,
  17282.         PK_COLUMN_NAME         = c1.name,
  17283.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17284.         PK_COLUMN_PROPID    = convert(int,null),
  17285.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17286.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17287.         FK_TABLE_NAME         = o2.name,
  17288.         FK_COLUMN_NAME         = c2.name,
  17289.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17290.         FK_COLUMN_PROPID    = convert(int,null),
  17291.         ORDINAL                = convert(int,9),
  17292.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17293.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17294.         PK_NAME                = i.name,
  17295.         FK_NAME                = object_name(r.constid),
  17296.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17297.     from
  17298.         sysobjects o1, sysobjects o2,
  17299.         syscolumns c1, syscolumns c2,
  17300.         sysreferences r, sysindexes i
  17301.     where    
  17302.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17303.     and    o1.name = @pk_table_name
  17304.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17305.     and    o1.id = r.rkeyid
  17306.     and    r.keycnt >= 9
  17307.     and    o1.id = c1.id
  17308.     and    c1.colid = r.rkey9
  17309.     and r.fkeyid = o2.id
  17310.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17311.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17312.     and    o2.id = c2.id    
  17313.     and    c2.colid = r.fkey9
  17314.     and permissions(o1.id) <> 0
  17315.     and permissions(o2.id) <> 0
  17316.     and i.id = r.rkeyid
  17317.     and i.indid = r.rkeyindid            
  17318.     union all
  17319.     select
  17320.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17321.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17322.         PK_TABLE_NAME         = o1.name,
  17323.         PK_COLUMN_NAME         = c1.name,
  17324.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17325.         PK_COLUMN_PROPID    = convert(int,null),
  17326.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17327.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17328.         FK_TABLE_NAME         = o2.name,
  17329.         FK_COLUMN_NAME         = c2.name,
  17330.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17331.         FK_COLUMN_PROPID    = convert(int,null),
  17332.         ORDINAL                = convert(int,10),
  17333.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17334.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17335.         PK_NAME                = i.name,
  17336.         FK_NAME                = object_name(r.constid),
  17337.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17338.     from
  17339.         sysobjects o1, sysobjects o2,
  17340.         syscolumns c1, syscolumns c2,
  17341.         sysreferences r, sysindexes i
  17342.     where    
  17343.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17344.     and    o1.name = @pk_table_name
  17345.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17346.     and    o1.id = r.rkeyid
  17347.     and    r.keycnt >= 10
  17348.     and    o1.id = c1.id
  17349.     and    c1.colid = r.rkey10
  17350.     and r.fkeyid = o2.id
  17351.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17352.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17353.     and    o2.id = c2.id    
  17354.     and    c2.colid = r.fkey10
  17355.     and permissions(o1.id) <> 0
  17356.     and permissions(o2.id) <> 0
  17357.     and i.id = r.rkeyid
  17358.     and i.indid = r.rkeyindid            
  17359.     union all
  17360.     select
  17361.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17362.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17363.         PK_TABLE_NAME         = o1.name,
  17364.         PK_COLUMN_NAME         = c1.name,
  17365.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17366.         PK_COLUMN_PROPID    = convert(int,null),
  17367.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17368.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17369.         FK_TABLE_NAME         = o2.name,
  17370.         FK_COLUMN_NAME         = c2.name,
  17371.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17372.         FK_COLUMN_PROPID    = convert(int,null),
  17373.         ORDINAL                = convert(int,11),
  17374.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17375.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17376.         PK_NAME                = i.name,
  17377.         FK_NAME                = object_name(r.constid),
  17378.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17379.     from
  17380.         sysobjects o1, sysobjects o2,
  17381.         syscolumns c1, syscolumns c2,
  17382.         sysreferences r, sysindexes i
  17383.     where    
  17384.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17385.     and    o1.name = @pk_table_name
  17386.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17387.     and    o1.id = r.rkeyid
  17388.     and    r.keycnt >= 11
  17389.     and    o1.id = c1.id
  17390.     and    c1.colid = r.rkey11
  17391.     and r.fkeyid = o2.id
  17392.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17393.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17394.     and    o2.id = c2.id    
  17395.     and    c2.colid = r.fkey11
  17396.     and permissions(o1.id) <> 0
  17397.     and permissions(o2.id) <> 0
  17398.     and i.id = r.rkeyid
  17399.     and i.indid = r.rkeyindid            
  17400.     union all
  17401.     select
  17402.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17403.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17404.         PK_TABLE_NAME         = o1.name,
  17405.         PK_COLUMN_NAME         = c1.name,
  17406.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17407.         PK_COLUMN_PROPID    = convert(int,null),
  17408.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17409.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17410.         FK_TABLE_NAME         = o2.name,
  17411.         FK_COLUMN_NAME         = c2.name,
  17412.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17413.         FK_COLUMN_PROPID    = convert(int,null),
  17414.         ORDINAL                = convert(int,12),
  17415.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17416.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17417.         PK_NAME                = i.name,
  17418.         FK_NAME                = object_name(r.constid),
  17419.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17420.     from
  17421.         sysobjects o1, sysobjects o2,
  17422.         syscolumns c1, syscolumns c2,
  17423.         sysreferences r, sysindexes i
  17424.     where    
  17425.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17426.     and    o1.name = @pk_table_name
  17427.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17428.     and    o1.id = r.rkeyid
  17429.     and    r.keycnt >= 12
  17430.     and    o1.id = c1.id
  17431.     and    c1.colid = r.rkey12
  17432.     and r.fkeyid = o2.id
  17433.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17434.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17435.     and    o2.id = c2.id    
  17436.     and    c2.colid = r.fkey12
  17437.     and permissions(o1.id) <> 0
  17438.     and permissions(o2.id) <> 0
  17439.     and i.id = r.rkeyid
  17440.     and i.indid = r.rkeyindid            
  17441.     union all
  17442.     select
  17443.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17444.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17445.         PK_TABLE_NAME         = o1.name,
  17446.         PK_COLUMN_NAME         = c1.name,
  17447.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17448.         PK_COLUMN_PROPID    = convert(int,null),
  17449.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17450.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17451.         FK_TABLE_NAME         = o2.name,
  17452.         FK_COLUMN_NAME         = c2.name,
  17453.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17454.         FK_COLUMN_PROPID    = convert(int,null),
  17455.         ORDINAL                = convert(int,13),
  17456.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17457.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17458.         PK_NAME                = i.name,
  17459.         FK_NAME                = object_name(r.constid),
  17460.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17461.     from
  17462.         sysobjects o1, sysobjects o2,
  17463.         syscolumns c1, syscolumns c2,
  17464.         sysreferences r, sysindexes i
  17465.     where    
  17466.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17467.     and    o1.name = @pk_table_name
  17468.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17469.     and    o1.id = r.rkeyid
  17470.     and    r.keycnt >= 13
  17471.     and    o1.id = c1.id
  17472.     and    c1.colid = r.rkey13
  17473.     and r.fkeyid = o2.id
  17474.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17475.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17476.     and    o2.id = c2.id    
  17477.     and    c2.colid = r.fkey13
  17478.     and permissions(o1.id) <> 0
  17479.     and permissions(o2.id) <> 0
  17480.     and i.id = r.rkeyid
  17481.     and i.indid = r.rkeyindid            
  17482.     union all
  17483.     select
  17484.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17485.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17486.         PK_TABLE_NAME         = o1.name,
  17487.         PK_COLUMN_NAME         = c1.name,
  17488.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17489.         PK_COLUMN_PROPID    = convert(int,null),
  17490.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17491.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17492.         FK_TABLE_NAME         = o2.name,
  17493.         FK_COLUMN_NAME         = c2.name,
  17494.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17495.         FK_COLUMN_PROPID    = convert(int,null),
  17496.         ORDINAL                = convert(int,14),
  17497.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17498.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17499.         PK_NAME                = i.name,
  17500.         FK_NAME                = object_name(r.constid),
  17501.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17502.     from
  17503.         sysobjects o1, sysobjects o2,
  17504.         syscolumns c1, syscolumns c2,
  17505.         sysreferences r, sysindexes i
  17506.     where    
  17507.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17508.     and    o1.name = @pk_table_name
  17509.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17510.     and    o1.id = r.rkeyid
  17511.     and    r.keycnt >= 14
  17512.     and    o1.id = c1.id
  17513.     and    c1.colid = r.rkey14
  17514.     and r.fkeyid = o2.id
  17515.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17516.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17517.     and    o2.id = c2.id    
  17518.     and    c2.colid = r.fkey14
  17519.     and permissions(o1.id) <> 0
  17520.     and permissions(o2.id) <> 0
  17521.     and i.id = r.rkeyid
  17522.     and i.indid = r.rkeyindid            
  17523.     union all
  17524.     select
  17525.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17526.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17527.         PK_TABLE_NAME         = o1.name,
  17528.         PK_COLUMN_NAME         = c1.name,
  17529.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17530.         PK_COLUMN_PROPID    = convert(int,null),
  17531.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17532.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17533.         FK_TABLE_NAME         = o2.name,
  17534.         FK_COLUMN_NAME         = c2.name,
  17535.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17536.         FK_COLUMN_PROPID    = convert(int,null),
  17537.         ORDINAL                = convert(int,15),
  17538.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17539.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17540.         PK_NAME                = i.name,
  17541.         FK_NAME                = object_name(r.constid),
  17542.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17543.     from
  17544.         sysobjects o1, sysobjects o2,
  17545.         syscolumns c1, syscolumns c2,
  17546.         sysreferences r, sysindexes i
  17547.     where    
  17548.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17549.     and    o1.name = @pk_table_name
  17550.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17551.     and    o1.id = r.rkeyid
  17552.     and    r.keycnt >= 15
  17553.     and    o1.id = c1.id
  17554.     and    c1.colid = r.rkey15
  17555.     and r.fkeyid = o2.id
  17556.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17557.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17558.     and    o2.id = c2.id    
  17559.     and    c2.colid = r.fkey15
  17560.     and permissions(o1.id) <> 0
  17561.     and permissions(o2.id) <> 0
  17562.     and i.id = r.rkeyid
  17563.     and i.indid = r.rkeyindid            
  17564.     union all
  17565.     select
  17566.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17567.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17568.         PK_TABLE_NAME         = o1.name,
  17569.         PK_COLUMN_NAME         = c1.name,
  17570.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17571.         PK_COLUMN_PROPID    = convert(int,null),
  17572.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17573.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17574.         FK_TABLE_NAME         = o2.name,
  17575.         FK_COLUMN_NAME         = c2.name,
  17576.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17577.         FK_COLUMN_PROPID    = convert(int,null),
  17578.         ORDINAL                = convert(int,16),
  17579.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17580.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17581.         PK_NAME                = i.name,
  17582.         FK_NAME                = object_name(r.constid),
  17583.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17584.     from
  17585.         sysobjects o1, sysobjects o2,
  17586.         syscolumns c1, syscolumns c2,
  17587.         sysreferences r, sysindexes i
  17588.     where    
  17589.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17590.     and    o1.name = @pk_table_name
  17591.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17592.     and    o1.id = r.rkeyid
  17593.     and    r.keycnt >= 16
  17594.     and    o1.id = c1.id
  17595.     and    c1.colid = r.rkey16
  17596.     and r.fkeyid = o2.id
  17597.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17598.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17599.     and    o2.id = c2.id    
  17600.     and    c2.colid = r.fkey16
  17601.     and permissions(o1.id) <> 0
  17602.     and permissions(o2.id) <> 0
  17603.     and i.id = r.rkeyid
  17604.     and i.indid = r.rkeyindid        
  17605.     order by 8,9,2,3,13
  17606. go
  17607. dump tran master with no_log
  17608. go
  17609. CREATE PROCEDURE sp_foreign_keys_rowset;2
  17610.     (
  17611.     @fk_table_name        sysname,
  17612.     @fk_table_schema    sysname = null,
  17613.     @pk_table_name        sysname = null,
  17614.     @pk_table_schema    sysname = null,
  17615.     @pk_table_catalog    sysname = null
  17616.     )
  17617. as
  17618.     select
  17619.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17620.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17621.         PK_TABLE_NAME         = o1.name,
  17622.         PK_COLUMN_NAME         = c1.name,
  17623.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17624.         PK_COLUMN_PROPID    = convert(int,null),
  17625.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17626.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17627.         FK_TABLE_NAME         = o2.name,
  17628.         FK_COLUMN_NAME         = c2.name,
  17629.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17630.         FK_COLUMN_PROPID    = convert(int,null),
  17631.         ORDINAL                = convert(int,1),
  17632.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17633.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17634.         PK_NAME                = i.name,
  17635.         FK_NAME                = object_name(r.constid),
  17636.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17637.     from
  17638.         sysobjects o1, sysobjects o2,
  17639.         syscolumns c1, syscolumns c2,
  17640.         sysreferences r, sysindexes i
  17641.     where    
  17642.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17643.     and    o2.name = @fk_table_name
  17644.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17645.     and    o2.id = r.fkeyid
  17646.     and    o2.id = c2.id
  17647.     and    c2.colid = r.fkey1
  17648.     and r.rkeyid = o1.id
  17649.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17650.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17651.     and    o1.id = c1.id    
  17652.     and    c1.colid = r.rkey1
  17653.     and permissions(o1.id) <> 0
  17654.     and permissions(o2.id) <> 0
  17655.     and i.id = r.rkeyid
  17656.     and i.indid = r.rkeyindid        
  17657.     union    all
  17658.     select
  17659.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17660.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17661.         PK_TABLE_NAME         = o1.name,
  17662.         PK_COLUMN_NAME         = c1.name,
  17663.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17664.         PK_COLUMN_PROPID    = convert(int,null),
  17665.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17666.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17667.         FK_TABLE_NAME         = o2.name,
  17668.         FK_COLUMN_NAME         = c2.name,
  17669.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17670.         FK_COLUMN_PROPID    = convert(int,null),
  17671.         ORDINAL                = convert(int,2),
  17672.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17673.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17674.         PK_NAME                = i.name,
  17675.         FK_NAME                = object_name(r.constid),
  17676.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17677.     from
  17678.         sysobjects o1, sysobjects o2,
  17679.         syscolumns c1, syscolumns c2,
  17680.         sysreferences r, sysindexes i
  17681.     where    
  17682.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17683.     and    o2.name = @fk_table_name
  17684.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17685.     and    o2.id = r.fkeyid
  17686.     and    r.keycnt >= 2
  17687.     and    o2.id = c2.id
  17688.     and    c2.colid = r.fkey2
  17689.     and    r.rkeyid = o1.id
  17690.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17691.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17692.     and    o1.id = c1.id    
  17693.     and    c1.colid = r.rkey2
  17694.     and permissions(o1.id) <> 0
  17695.     and permissions(o2.id) <> 0
  17696.     and i.id = r.rkeyid
  17697.     and i.indid = r.rkeyindid            
  17698.     union    all
  17699.     select
  17700.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17701.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17702.         PK_TABLE_NAME         = o1.name,
  17703.         PK_COLUMN_NAME         = c1.name,
  17704.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17705.         PK_COLUMN_PROPID    = convert(int,null),
  17706.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17707.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17708.         FK_TABLE_NAME         = o2.name,
  17709.         FK_COLUMN_NAME         = c2.name,
  17710.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17711.         FK_COLUMN_PROPID    = convert(int,null),
  17712.         ORDINAL                = convert(int,3),
  17713.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17714.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17715.         PK_NAME                = i.name,
  17716.         FK_NAME                = object_name(r.constid),
  17717.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17718.     from
  17719.         sysobjects o1, sysobjects o2,
  17720.         syscolumns c1, syscolumns c2,
  17721.         sysreferences r, sysindexes i
  17722.     where    
  17723.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17724.     and    o2.name = @fk_table_name
  17725.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17726.     and    o2.id = r.fkeyid
  17727.     and    r.keycnt >= 3
  17728.     and    o2.id = c2.id
  17729.     and    c2.colid = r.fkey3
  17730.     and r.rkeyid = o1.id
  17731.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17732.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17733.     and    o1.id = c1.id    
  17734.     and    c1.colid = r.rkey3
  17735.     and permissions(o1.id) <> 0
  17736.     and permissions(o2.id) <> 0
  17737.     and i.id = r.rkeyid
  17738.     and i.indid = r.rkeyindid            
  17739.     union    all
  17740.     select
  17741.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17742.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17743.         PK_TABLE_NAME         = o1.name,
  17744.         PK_COLUMN_NAME         = c1.name,
  17745.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17746.         PK_COLUMN_PROPID    = convert(int,null),
  17747.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17748.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17749.         FK_TABLE_NAME         = o2.name,
  17750.         FK_COLUMN_NAME         = c2.name,
  17751.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17752.         FK_COLUMN_PROPID    = convert(int,null),
  17753.         ORDINAL                = convert(int,4),
  17754.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17755.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17756.         PK_NAME                = i.name,
  17757.         FK_NAME                = object_name(r.constid),
  17758.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17759.     from
  17760.         sysobjects o1, sysobjects o2,
  17761.         syscolumns c1, syscolumns c2,
  17762.         sysreferences r, sysindexes i
  17763.     where    
  17764.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17765.     and    o2.name = @fk_table_name
  17766.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17767.     and    o2.id = r.fkeyid
  17768.     and    r.keycnt >= 4
  17769.     and    o2.id = c2.id
  17770.     and    c2.colid = r.fkey4
  17771.     and r.rkeyid = o1.id
  17772.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17773.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17774.     and    o1.id = c1.id    
  17775.     and    c1.colid = r.rkey4
  17776.     and permissions(o1.id) <> 0
  17777.     and permissions(o2.id) <> 0
  17778.     and i.id = r.rkeyid
  17779.     and i.indid = r.rkeyindid            
  17780.     union    all
  17781.     select
  17782.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17783.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17784.         PK_TABLE_NAME         = o1.name,
  17785.         PK_COLUMN_NAME         = c1.name,
  17786.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17787.         PK_COLUMN_PROPID    = convert(int,null),
  17788.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17789.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17790.         FK_TABLE_NAME         = o2.name,
  17791.         FK_COLUMN_NAME         = c2.name,
  17792.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17793.         FK_COLUMN_PROPID    = convert(int,null),
  17794.         ORDINAL                = convert(int,5),
  17795.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17796.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17797.         PK_NAME                = i.name,
  17798.         FK_NAME                = object_name(r.constid),
  17799.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17800.     from
  17801.         sysobjects o1, sysobjects o2,
  17802.         syscolumns c1, syscolumns c2,
  17803.         sysreferences r, sysindexes i
  17804.     where    
  17805.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17806.     and    o2.name = @fk_table_name
  17807.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17808.     and    o2.id = r.fkeyid
  17809.     and    r.keycnt >= 5
  17810.     and    o2.id = c2.id
  17811.     and    c2.colid = r.fkey5
  17812.     and    r.rkeyid = o1.id
  17813.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17814.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17815.     and    o1.id = c1.id    
  17816.     and    c1.colid = r.rkey5
  17817.     and permissions(o1.id) <> 0
  17818.     and permissions(o2.id) <> 0
  17819.     and i.id = r.rkeyid
  17820.     and i.indid = r.rkeyindid            
  17821.     union    all
  17822.     select
  17823.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17824.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17825.         PK_TABLE_NAME         = o1.name,
  17826.         PK_COLUMN_NAME         = c1.name,
  17827.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17828.         PK_COLUMN_PROPID    = convert(int,null),
  17829.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17830.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17831.         FK_TABLE_NAME         = o2.name,
  17832.         FK_COLUMN_NAME         = c2.name,
  17833.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17834.         FK_COLUMN_PROPID    = convert(int,null),
  17835.         ORDINAL                = convert(int,6),
  17836.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17837.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17838.         PK_NAME                = i.name,
  17839.         FK_NAME                = object_name(r.constid),
  17840.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17841.     from
  17842.         sysobjects o1, sysobjects o2,
  17843.         syscolumns c1, syscolumns c2,
  17844.         sysreferences r, sysindexes i
  17845.     where    
  17846.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17847.     and    o2.name = @fk_table_name
  17848.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17849.     and    o2.id = r.fkeyid
  17850.     and    r.keycnt >= 6
  17851.     and    o2.id = c2.id
  17852.     and    c2.colid = r.fkey6
  17853.     and r.rkeyid = o1.id
  17854.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17855.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17856.     and    o1.id = c1.id        
  17857.     and    c1.colid = r.rkey6
  17858.     and permissions(o1.id) <> 0
  17859.     and permissions(o2.id) <> 0
  17860.     and i.id = r.rkeyid
  17861.     and i.indid = r.rkeyindid        
  17862.     union    all
  17863.     select
  17864.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17865.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17866.         PK_TABLE_NAME         = o1.name,
  17867.         PK_COLUMN_NAME         = c1.name,
  17868.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17869.         PK_COLUMN_PROPID    = convert(int,null),
  17870.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17871.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17872.         FK_TABLE_NAME         = o2.name,
  17873.         FK_COLUMN_NAME         = c2.name,
  17874.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17875.         FK_COLUMN_PROPID    = convert(int,null),
  17876.         ORDINAL                = convert(int,7),
  17877.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17878.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17879.         PK_NAME                = i.name,
  17880.         FK_NAME                = object_name(r.constid),
  17881.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17882.     from
  17883.         sysobjects o1, sysobjects o2,
  17884.         syscolumns c1, syscolumns c2,
  17885.         sysreferences r, sysindexes i
  17886.     where    
  17887.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17888.     and    o2.name = @fk_table_name
  17889.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17890.     and    o2.id = r.fkeyid
  17891.     and    r.keycnt >= 7
  17892.     and    o2.id = c2.id
  17893.     and    c2.colid = r.fkey7
  17894.     and    r.rkeyid = o1.id
  17895.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17896.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17897.     and    o1.id = c1.id    
  17898.     and    c1.colid = r.rkey7
  17899.     and permissions(o1.id) <> 0
  17900.     and permissions(o2.id) <> 0
  17901.     and i.id = r.rkeyid
  17902.     and i.indid = r.rkeyindid            
  17903.     union    all
  17904.     select
  17905.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17906.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17907.         PK_TABLE_NAME         = o1.name,
  17908.         PK_COLUMN_NAME         = c1.name,
  17909.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17910.         PK_COLUMN_PROPID    = convert(int,null),
  17911.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17912.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17913.         FK_TABLE_NAME         = o2.name,
  17914.         FK_COLUMN_NAME         = c2.name,
  17915.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17916.         FK_COLUMN_PROPID    = convert(int,null),
  17917.         ORDINAL                = convert(int,8),
  17918.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17919.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17920.         PK_NAME                = i.name,
  17921.         FK_NAME                = object_name(r.constid),
  17922.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17923.     from
  17924.         sysobjects o1, sysobjects o2,
  17925.         syscolumns c1, syscolumns c2,
  17926.         sysreferences r, sysindexes i
  17927.     where    
  17928.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17929.     and    o2.name = @fk_table_name
  17930.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17931.     and    o2.id = r.fkeyid
  17932.     and    r.keycnt >= 8
  17933.     and    o2.id = c2.id
  17934.     and    c2.colid = r.fkey8
  17935.     and r.rkeyid = o1.id
  17936.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17937.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17938.     and    o1.id = c1.id    
  17939.     and    c1.colid = r.rkey8
  17940.     and permissions(o1.id) <> 0
  17941.     and permissions(o2.id) <> 0
  17942.     and i.id = r.rkeyid
  17943.     and i.indid = r.rkeyindid            
  17944.     union    all
  17945.     select
  17946.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17947.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17948.         PK_TABLE_NAME         = o1.name,
  17949.         PK_COLUMN_NAME         = c1.name,
  17950.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17951.         PK_COLUMN_PROPID    = convert(int,null),
  17952.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17953.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17954.         FK_TABLE_NAME         = o2.name,
  17955.         FK_COLUMN_NAME         = c2.name,
  17956.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17957.         FK_COLUMN_PROPID    = convert(int,null),
  17958.         ORDINAL                = convert(int,9),
  17959.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17960.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17961.         PK_NAME                = i.name,
  17962.         FK_NAME                = object_name(r.constid),
  17963.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17964.     from
  17965.         sysobjects o1, sysobjects o2,
  17966.         syscolumns c1, syscolumns c2,
  17967.         sysreferences r, sysindexes i
  17968.     where    
  17969.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17970.     and    o2.name = @fk_table_name
  17971.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17972.     and    o2.id = r.fkeyid
  17973.     and    r.keycnt >= 9
  17974.     and    o2.id = c2.id
  17975.     and    c2.colid = r.fkey9
  17976.     and    r.rkeyid = o1.id
  17977.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17978.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17979.     and    o1.id = c1.id    
  17980.     and    c1.colid = r.rkey9
  17981.     and permissions(o1.id) <> 0
  17982.     and permissions(o2.id) <> 0
  17983.     and i.id = r.rkeyid
  17984.     and i.indid = r.rkeyindid            
  17985.     union    all
  17986.     select
  17987.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17988.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17989.         PK_TABLE_NAME         = o1.name,
  17990.         PK_COLUMN_NAME         = c1.name,
  17991.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17992.         PK_COLUMN_PROPID    = convert(int,null),
  17993.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17994.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17995.         FK_TABLE_NAME         = o2.name,
  17996.         FK_COLUMN_NAME         = c2.name,
  17997.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17998.         FK_COLUMN_PROPID    = convert(int,null),
  17999.         ORDINAL                = convert(int,10),
  18000.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18001.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18002.         PK_NAME                = i.name,
  18003.         FK_NAME                = object_name(r.constid),
  18004.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18005.     from
  18006.         sysobjects o1, sysobjects o2,
  18007.         syscolumns c1, syscolumns c2,
  18008.         sysreferences r, sysindexes i
  18009.     where    
  18010.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18011.     and    o2.name = @fk_table_name
  18012.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18013.     and    o2.id = r.fkeyid
  18014.     and    r.keycnt >= 10
  18015.     and    o2.id = c2.id
  18016.     and    c2.colid = r.fkey10
  18017.     and r.rkeyid = o1.id
  18018.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18019.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18020.     and    o1.id = c1.id    
  18021.     and    c1.colid = r.rkey10
  18022.     and permissions(o1.id) <> 0
  18023.     and permissions(o2.id) <> 0
  18024.     and i.id = r.rkeyid
  18025.     and i.indid = r.rkeyindid            
  18026.     union    all
  18027.     select
  18028.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18029.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18030.         PK_TABLE_NAME         = o1.name,
  18031.         PK_COLUMN_NAME         = c1.name,
  18032.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18033.         PK_COLUMN_PROPID    = convert(int,null),
  18034.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18035.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18036.         FK_TABLE_NAME         = o2.name,
  18037.         FK_COLUMN_NAME         = c2.name,
  18038.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18039.         FK_COLUMN_PROPID    = convert(int,null),
  18040.         ORDINAL                = convert(int,11),
  18041.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18042.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18043.         PK_NAME                = i.name,
  18044.         FK_NAME                = object_name(r.constid),
  18045.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18046.     from
  18047.         sysobjects o1, sysobjects o2,
  18048.         syscolumns c1, syscolumns c2,
  18049.         sysreferences r, sysindexes i
  18050.     where    
  18051.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18052.     and    o2.name = @fk_table_name
  18053.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18054.     and    o2.id = r.fkeyid
  18055.     and    r.keycnt >= 11
  18056.     and    o2.id = c2.id
  18057.     and    c2.colid = r.fkey11
  18058.     and r.rkeyid = o1.id
  18059.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18060.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18061.     and    o1.id = c1.id    
  18062.     and    c1.colid = r.rkey11
  18063.     and permissions(o1.id) <> 0
  18064.     and permissions(o2.id) <> 0
  18065.     and i.id = r.rkeyid
  18066.     and i.indid = r.rkeyindid            
  18067.     union    all
  18068.     select
  18069.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18070.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18071.         PK_TABLE_NAME         = o1.name,
  18072.         PK_COLUMN_NAME         = c1.name,
  18073.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18074.         PK_COLUMN_PROPID    = convert(int,null),
  18075.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18076.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18077.         FK_TABLE_NAME         = o2.name,
  18078.         FK_COLUMN_NAME         = c2.name,
  18079.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18080.         FK_COLUMN_PROPID    = convert(int,null),
  18081.         ORDINAL                = convert(int,12),
  18082.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18083.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18084.         PK_NAME                = i.name,
  18085.         FK_NAME                = object_name(r.constid),
  18086.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18087.     from
  18088.         sysobjects o1, sysobjects o2,
  18089.         syscolumns c1, syscolumns c2,
  18090.         sysreferences r, sysindexes i
  18091.     where    
  18092.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18093.     and    o2.name = @fk_table_name
  18094.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18095.     and    o2.id = r.fkeyid
  18096.     and    r.keycnt >= 12
  18097.     and    o2.id = c2.id
  18098.     and    c2.colid = r.fkey12
  18099.     and r.rkeyid = o1.id
  18100.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18101.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18102.     and    o1.id = c1.id    
  18103.     and    c1.colid = r.rkey12
  18104.     and permissions(o1.id) <> 0
  18105.     and permissions(o2.id) <> 0
  18106.     and i.id = r.rkeyid
  18107.     and i.indid = r.rkeyindid            
  18108.     union    all
  18109.     select
  18110.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18111.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18112.         PK_TABLE_NAME         = o1.name,
  18113.         PK_COLUMN_NAME         = c1.name,
  18114.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18115.         PK_COLUMN_PROPID    = convert(int,null),
  18116.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18117.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18118.         FK_TABLE_NAME         = o2.name,
  18119.         FK_COLUMN_NAME         = c2.name,
  18120.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18121.         FK_COLUMN_PROPID    = convert(int,null),
  18122.         ORDINAL                = convert(int,13),
  18123.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18124.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18125.         PK_NAME                = i.name,
  18126.         FK_NAME                = object_name(r.constid),
  18127.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18128.     from
  18129.         sysobjects o1, sysobjects o2,
  18130.         syscolumns c1, syscolumns c2,
  18131.         sysreferences r, sysindexes i
  18132.     where    
  18133.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18134.     and    o2.name = @fk_table_name
  18135.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18136.     and    o2.id = r.fkeyid
  18137.     and    r.keycnt >= 13
  18138.     and    o2.id = c2.id
  18139.     and    c2.colid = r.fkey13
  18140.     and r.rkeyid = o1.id
  18141.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18142.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18143.     and    o1.id = c1.id    
  18144.     and    c1.colid = r.rkey13
  18145.     and permissions(o1.id) <> 0
  18146.     and permissions(o2.id) <> 0
  18147.     and i.id = r.rkeyid
  18148.     and i.indid = r.rkeyindid            
  18149.     union    all
  18150.     select
  18151.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18152.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18153.         PK_TABLE_NAME         = o1.name,
  18154.         PK_COLUMN_NAME         = c1.name,
  18155.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18156.         PK_COLUMN_PROPID    = convert(int,null),
  18157.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18158.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18159.         FK_TABLE_NAME         = o2.name,
  18160.         FK_COLUMN_NAME         = c2.name,
  18161.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18162.         FK_COLUMN_PROPID    = convert(int,null),
  18163.         ORDINAL                = convert(int,14),
  18164.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18165.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18166.         PK_NAME                = i.name,
  18167.         FK_NAME                = object_name(r.constid),
  18168.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18169.     from
  18170.         sysobjects o1, sysobjects o2,
  18171.         syscolumns c1, syscolumns c2,
  18172.         sysreferences r, sysindexes i
  18173.     where    
  18174.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18175.     and    o2.name = @fk_table_name
  18176.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18177.     and    o2.id = r.fkeyid
  18178.     and    r.keycnt >= 14
  18179.     and    o2.id = c2.id
  18180.     and    c2.colid = r.fkey14
  18181.     and r.rkeyid = o1.id
  18182.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18183.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18184.     and    o1.id = c1.id    
  18185.     and    c1.colid = r.rkey14
  18186.     and permissions(o1.id) <> 0
  18187.     and permissions(o2.id) <> 0
  18188.     and i.id = r.rkeyid
  18189.     and i.indid = r.rkeyindid            
  18190.     union    all
  18191.     select
  18192.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18193.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18194.         PK_TABLE_NAME         = o1.name,
  18195.         PK_COLUMN_NAME         = c1.name,
  18196.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18197.         PK_COLUMN_PROPID    = convert(int,null),
  18198.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18199.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18200.         FK_TABLE_NAME         = o2.name,
  18201.         FK_COLUMN_NAME         = c2.name,
  18202.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18203.         FK_COLUMN_PROPID    = convert(int,null),
  18204.         ORDINAL                = convert(int,15),
  18205.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18206.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18207.         PK_NAME                = i.name,
  18208.         FK_NAME                = object_name(r.constid),
  18209.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  18210.     from
  18211.         sysobjects o1, sysobjects o2,
  18212.         syscolumns c1, syscolumns c2,
  18213.         sysreferences r, sysindexes i
  18214.     where    
  18215.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18216.     and    o2.name = @fk_table_name
  18217.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18218.     and    o2.id = r.fkeyid
  18219.     and    r.keycnt >= 15
  18220.     and    o2.id = c2.id
  18221.     and    c2.colid = r.fkey15
  18222.     and r.rkeyid = o1.id
  18223.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18224.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18225.     and    o1.id = c1.id    
  18226.     and    c1.colid = r.rkey15
  18227.     and permissions(o1.id) <> 0
  18228.     and permissions(o2.id) <> 0
  18229.     and i.id = r.rkeyid
  18230.     and i.indid = r.rkeyindid            
  18231.     union    all
  18232.     select
  18233.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18234.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18235.         PK_TABLE_NAME         = o1.name,
  18236.         PK_COLUMN_NAME         = c1.name,
  18237.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18238.         PK_COLUMN_PROPID    = convert(int,null),
  18239.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18240.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18241.         FK_TABLE_NAME         = o2.name,
  18242.         FK_COLUMN_NAME         = c2.name,
  18243.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18244.         FK_COLUMN_PROPID    = convert(int,null),
  18245.         ORDINAL                = convert(int,16),
  18246.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18247.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18248.         PK_NAME                = i.name,
  18249.         FK_NAME                = object_name(r.constid),
  18250.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  18251.     from
  18252.         sysobjects o1, sysobjects o2,
  18253.         syscolumns c1, syscolumns c2,
  18254.         sysreferences r, sysindexes i
  18255.     where    
  18256.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18257.     and    o2.name = @fk_table_name
  18258.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18259.     and    o2.id = r.fkeyid
  18260.     and    r.keycnt >= 16
  18261.     and    o2.id = c2.id
  18262.     and    c2.colid = r.fkey16
  18263.     and r.rkeyid = o1.id
  18264.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18265.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18266.     and    o1.id = c1.id    
  18267.     and    c1.colid = r.rkey16
  18268.     and permissions(o1.id) <> 0
  18269.     and permissions(o2.id) <> 0
  18270.     and i.id = r.rkeyid
  18271.     and i.indid = r.rkeyindid        
  18272.     order by 8,9,2,3,13
  18273. go
  18274. dump tran master with no_log
  18275. go
  18276. CREATE PROCEDURE sp_foreign_keys_rowset;3
  18277.     (
  18278.     @pk_table_schema    sysname = null,
  18279.     @pk_table_catalog    sysname = null,
  18280.     @fk_table_schema    sysname = null,
  18281.     @fk_table_catalog    sysname = null
  18282.     )
  18283. as
  18284.     select
  18285.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18286.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18287.         PK_TABLE_NAME         = o1.name,
  18288.         PK_COLUMN_NAME         = c1.name,
  18289.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18290.         PK_COLUMN_PROPID    = convert(int,null),
  18291.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18292.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18293.         FK_TABLE_NAME         = o2.name,
  18294.         FK_COLUMN_NAME         = c2.name,
  18295.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18296.         FK_COLUMN_PROPID    = convert(int,null),
  18297.         ORDINAL                = convert(int,1),
  18298.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18299.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18300.         PK_NAME                = i.name,
  18301.         FK_NAME                = object_name(r.constid),
  18302.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  18303.     from
  18304.         sysobjects o1, sysobjects o2,
  18305.         syscolumns c1, syscolumns c2,
  18306.         sysreferences r, sysindexes i
  18307.     where    
  18308.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18309.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18310.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18311.     and    o1.id = r.rkeyid
  18312.     and    o1.id = c1.id
  18313.     and    c1.colid = r.rkey1
  18314.     and r.fkeyid = o2.id
  18315.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18316.     and    o2.id = c2.id    
  18317.     and    c2.colid = r.fkey1
  18318.     and permissions(o1.id) <> 0
  18319.     and permissions(o2.id) <> 0
  18320.     and i.id = r.rkeyid
  18321.     and i.indid = r.rkeyindid        
  18322.     union all
  18323.     select
  18324.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18325.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18326.         PK_TABLE_NAME         = o1.name,
  18327.         PK_COLUMN_NAME         = c1.name,
  18328.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18329.         PK_COLUMN_PROPID    = convert(int,null),
  18330.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18331.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18332.         FK_TABLE_NAME         = o2.name,
  18333.         FK_COLUMN_NAME         = c2.name,
  18334.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18335.         FK_COLUMN_PROPID    = convert(int,null),
  18336.         ORDINAL                = convert(int,2),
  18337.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18338.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18339.         PK_NAME                = i.name,
  18340.         FK_NAME                = object_name(r.constid),
  18341.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18342.     from
  18343.         sysobjects o1, sysobjects o2,
  18344.         syscolumns c1, syscolumns c2,
  18345.         sysreferences r, sysindexes i
  18346.     where    
  18347.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18348.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18349.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18350.     and    o1.id = r.rkeyid
  18351.     and    r.keycnt >= 2
  18352.     and    o1.id = c1.id
  18353.     and    c1.colid = r.rkey2
  18354.     and r.fkeyid = o2.id
  18355.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18356.     and    o2.id = c2.id    
  18357.     and    c2.colid = r.fkey2
  18358.     and permissions(o1.id) <> 0
  18359.     and permissions(o2.id) <> 0
  18360.     and i.id = r.rkeyid
  18361.     and i.indid = r.rkeyindid        
  18362.     union all
  18363.     select
  18364.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18365.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18366.         PK_TABLE_NAME         = o1.name,
  18367.         PK_COLUMN_NAME         = c1.name,
  18368.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18369.         PK_COLUMN_PROPID    = convert(int,null),
  18370.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18371.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18372.         FK_TABLE_NAME         = o2.name,
  18373.         FK_COLUMN_NAME         = c2.name,
  18374.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18375.         FK_COLUMN_PROPID    = convert(int,null),
  18376.         ORDINAL                = convert(int,3),
  18377.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18378.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18379.         PK_NAME                = i.name,
  18380.         FK_NAME                = object_name(r.constid),
  18381.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18382.     from
  18383.         sysobjects o1, sysobjects o2,
  18384.         syscolumns c1, syscolumns c2,
  18385.         sysreferences r, sysindexes i
  18386.     where    
  18387.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18388.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18389.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18390.     and    o1.id = r.rkeyid
  18391.     and    r.keycnt >= 3
  18392.     and    o1.id = c1.id
  18393.     and    c1.colid = r.rkey3
  18394.     and r.fkeyid = o2.id
  18395.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18396.     and    o2.id = c2.id    
  18397.     and    c2.colid = r.fkey3
  18398.     and permissions(o1.id) <> 0
  18399.     and permissions(o2.id) <> 0
  18400.     and i.id = r.rkeyid
  18401.     and i.indid = r.rkeyindid        
  18402.     union all
  18403.     select
  18404.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18405.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18406.         PK_TABLE_NAME         = o1.name,
  18407.         PK_COLUMN_NAME         = c1.name,
  18408.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18409.         PK_COLUMN_PROPID    = convert(int,null),
  18410.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18411.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18412.         FK_TABLE_NAME         = o2.name,
  18413.         FK_COLUMN_NAME         = c2.name,
  18414.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18415.         FK_COLUMN_PROPID    = convert(int,null),
  18416.         ORDINAL                = convert(int,4),
  18417.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18418.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18419.         PK_NAME                = i.name,
  18420.         FK_NAME                = object_name(r.constid),
  18421.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18422.     from
  18423.         sysobjects o1, sysobjects o2,
  18424.         syscolumns c1, syscolumns c2,
  18425.         sysreferences r, sysindexes i
  18426.     where    
  18427.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18428.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18429.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18430.     and    o1.id = r.rkeyid
  18431.     and    r.keycnt >= 4
  18432.     and    o1.id = c1.id
  18433.     and    c1.colid = r.rkey4
  18434.     and r.fkeyid = o2.id
  18435.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18436.     and    o2.id = c2.id    
  18437.     and    c2.colid = r.fkey4
  18438.     and permissions(o1.id) <> 0
  18439.     and permissions(o2.id) <> 0
  18440.     and i.id = r.rkeyid
  18441.     and i.indid = r.rkeyindid        
  18442.     union all
  18443.     select
  18444.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18445.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18446.         PK_TABLE_NAME         = o1.name,
  18447.         PK_COLUMN_NAME         = c1.name,
  18448.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18449.         PK_COLUMN_PROPID    = convert(int,null),
  18450.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18451.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18452.         FK_TABLE_NAME         = o2.name,
  18453.         FK_COLUMN_NAME         = c2.name,
  18454.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18455.         FK_COLUMN_PROPID    = convert(int,null),
  18456.         ORDINAL                = convert(int,5),
  18457.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18458.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18459.         PK_NAME                = i.name,
  18460.         FK_NAME                = object_name(r.constid),
  18461.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18462.     from
  18463.         sysobjects o1, sysobjects o2,
  18464.         syscolumns c1, syscolumns c2,
  18465.         sysreferences r, sysindexes i
  18466.     where    
  18467.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18468.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18469.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18470.     and    o1.id = r.rkeyid
  18471.     and    r.keycnt >= 5
  18472.     and    o1.id = c1.id
  18473.     and    c1.colid = r.rkey5
  18474.     and r.fkeyid = o2.id
  18475.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18476.     and    o2.id = c2.id    
  18477.     and    c2.colid = r.fkey5
  18478.     and permissions(o1.id) <> 0
  18479.     and permissions(o2.id) <> 0
  18480.     and i.id = r.rkeyid
  18481.     and i.indid = r.rkeyindid        
  18482.     union all
  18483.     select
  18484.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18485.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18486.         PK_TABLE_NAME         = o1.name,
  18487.         PK_COLUMN_NAME         = c1.name,
  18488.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18489.         PK_COLUMN_PROPID    = convert(int,null),
  18490.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18491.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18492.         FK_TABLE_NAME         = o2.name,
  18493.         FK_COLUMN_NAME         = c2.name,
  18494.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18495.         FK_COLUMN_PROPID    = convert(int,null),
  18496.         ORDINAL                = convert(int,6),
  18497.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18498.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18499.         PK_NAME                = i.name,
  18500.         FK_NAME                = object_name(r.constid),
  18501.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18502.     from
  18503.         sysobjects o1, sysobjects o2,
  18504.         syscolumns c1, syscolumns c2,
  18505.         sysreferences r, sysindexes i
  18506.     where    
  18507.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18508.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18509.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18510.     and    o1.id = r.rkeyid
  18511.     and    r.keycnt >= 6
  18512.     and    o1.id = c1.id
  18513.     and    c1.colid = r.rkey6
  18514.     and r.fkeyid = o2.id
  18515.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18516.     and    o2.id = c2.id    
  18517.     and    c2.colid = r.fkey6
  18518.     and permissions(o1.id) <> 0
  18519.     and permissions(o2.id) <> 0
  18520.     and i.id = r.rkeyid
  18521.     and i.indid = r.rkeyindid        
  18522.     union all
  18523.     select
  18524.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18525.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18526.         PK_TABLE_NAME         = o1.name,
  18527.         PK_COLUMN_NAME         = c1.name,
  18528.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18529.         PK_COLUMN_PROPID    = convert(int,null),
  18530.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18531.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18532.         FK_TABLE_NAME         = o2.name,
  18533.         FK_COLUMN_NAME         = c2.name,
  18534.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18535.         FK_COLUMN_PROPID    = convert(int,null),
  18536.         ORDINAL                = convert(int,7),
  18537.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18538.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18539.         PK_NAME                = i.name,
  18540.         FK_NAME                = object_name(r.constid),
  18541.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18542.     from
  18543.         sysobjects o1, sysobjects o2,
  18544.         syscolumns c1, syscolumns c2,
  18545.         sysreferences r, sysindexes i
  18546.     where    
  18547.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18548.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18549.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18550.     and    o1.id = r.rkeyid
  18551.     and    r.keycnt >= 7
  18552.     and    o1.id = c1.id
  18553.     and    c1.colid = r.rkey7
  18554.     and r.fkeyid = o2.id
  18555.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18556.     and    o2.id = c2.id    
  18557.     and    c2.colid = r.fkey7
  18558.     and permissions(o1.id) <> 0
  18559.     and permissions(o2.id) <> 0
  18560.     and i.id = r.rkeyid
  18561.     and i.indid = r.rkeyindid        
  18562.     union all
  18563.     select
  18564.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18565.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18566.         PK_TABLE_NAME         = o1.name,
  18567.         PK_COLUMN_NAME         = c1.name,
  18568.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18569.         PK_COLUMN_PROPID    = convert(int,null),
  18570.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18571.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18572.         FK_TABLE_NAME         = o2.name,
  18573.         FK_COLUMN_NAME         = c2.name,
  18574.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18575.         FK_COLUMN_PROPID    = convert(int,null),
  18576.         ORDINAL                = convert(int,8),
  18577.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18578.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18579.         PK_NAME                = i.name,
  18580.         FK_NAME                = object_name(r.constid),
  18581.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18582.     from
  18583.         sysobjects o1, sysobjects o2,
  18584.         syscolumns c1, syscolumns c2,
  18585.         sysreferences r, sysindexes i
  18586.     where    
  18587.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18588.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18589.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18590.     and    o1.id = r.rkeyid
  18591.     and    r.keycnt >= 8
  18592.     and    o1.id = c1.id
  18593.     and    c1.colid = r.rkey8
  18594.     and r.fkeyid = o2.id
  18595.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18596.     and    o2.id = c2.id    
  18597.     and    c2.colid = r.fkey8
  18598.     and permissions(o1.id) <> 0
  18599.     and permissions(o2.id) <> 0
  18600.     and i.id = r.rkeyid
  18601.     and i.indid = r.rkeyindid        
  18602.     union all
  18603.     select
  18604.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18605.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18606.         PK_TABLE_NAME         = o1.name,
  18607.         PK_COLUMN_NAME         = c1.name,
  18608.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18609.         PK_COLUMN_PROPID    = convert(int,null),
  18610.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18611.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18612.         FK_TABLE_NAME         = o2.name,
  18613.         FK_COLUMN_NAME         = c2.name,
  18614.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18615.         FK_COLUMN_PROPID    = convert(int,null),
  18616.         ORDINAL                = convert(int,9),
  18617.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18618.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18619.         PK_NAME                = i.name,
  18620.         FK_NAME                = object_name(r.constid),
  18621.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18622.     from
  18623.         sysobjects o1, sysobjects o2,
  18624.         syscolumns c1, syscolumns c2,
  18625.         sysreferences r, sysindexes i
  18626.     where    
  18627.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18628.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18629.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18630.     and    o1.id = r.rkeyid
  18631.     and    r.keycnt >= 9
  18632.     and    o1.id = c1.id
  18633.     and    c1.colid = r.rkey9
  18634.     and r.fkeyid = o2.id
  18635.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18636.     and    o2.id = c2.id    
  18637.     and    c2.colid = r.fkey9
  18638.     and permissions(o1.id) <> 0
  18639.     and permissions(o2.id) <> 0
  18640.     and i.id = r.rkeyid
  18641.     and i.indid = r.rkeyindid        
  18642.     union all
  18643.     select
  18644.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18645.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18646.         PK_TABLE_NAME         = o1.name,
  18647.         PK_COLUMN_NAME         = c1.name,
  18648.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18649.         PK_COLUMN_PROPID    = convert(int,null),
  18650.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18651.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18652.         FK_TABLE_NAME         = o2.name,
  18653.         FK_COLUMN_NAME         = c2.name,
  18654.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18655.         FK_COLUMN_PROPID    = convert(int,null),
  18656.         ORDINAL                = convert(int,10),
  18657.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18658.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18659.         PK_NAME                = i.name,
  18660.         FK_NAME                = object_name(r.constid),
  18661.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18662.     from
  18663.         sysobjects o1, sysobjects o2,
  18664.         syscolumns c1, syscolumns c2,
  18665.         sysreferences r, sysindexes i
  18666.     where    
  18667.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18668.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18669.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18670.     and    o1.id = r.rkeyid
  18671.     and    r.keycnt >= 10
  18672.     and    o1.id = c1.id
  18673.     and    c1.colid = r.rkey10
  18674.     and r.fkeyid = o2.id
  18675.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18676.     and    o2.id = c2.id    
  18677.     and    c2.colid = r.fkey10
  18678.     and permissions(o1.id) <> 0
  18679.     and permissions(o2.id) <> 0
  18680.     and i.id = r.rkeyid
  18681.     and i.indid = r.rkeyindid        
  18682.     union all
  18683.     select
  18684.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18685.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18686.         PK_TABLE_NAME         = o1.name,
  18687.         PK_COLUMN_NAME         = c1.name,
  18688.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18689.         PK_COLUMN_PROPID    = convert(int,null),
  18690.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18691.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18692.         FK_TABLE_NAME         = o2.name,
  18693.         FK_COLUMN_NAME         = c2.name,
  18694.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18695.         FK_COLUMN_PROPID    = convert(int,null),
  18696.         ORDINAL                = convert(int,11),
  18697.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18698.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18699.         PK_NAME                = i.name,
  18700.         FK_NAME                = object_name(r.constid),
  18701.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18702.     from
  18703.         sysobjects o1, sysobjects o2,
  18704.         syscolumns c1, syscolumns c2,
  18705.         sysreferences r, sysindexes i
  18706.     where    
  18707.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18708.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18709.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18710.     and    o1.id = r.rkeyid
  18711.     and    r.keycnt >= 11
  18712.     and    o1.id = c1.id
  18713.     and    c1.colid = r.rkey11
  18714.     and r.fkeyid = o2.id
  18715.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18716.     and    o2.id = c2.id    
  18717.     and    c2.colid = r.fkey11
  18718.     and permissions(o1.id) <> 0
  18719.     and permissions(o2.id) <> 0
  18720.     and i.id = r.rkeyid
  18721.     and i.indid = r.rkeyindid        
  18722.     union all
  18723.     select
  18724.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18725.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18726.         PK_TABLE_NAME         = o1.name,
  18727.         PK_COLUMN_NAME         = c1.name,
  18728.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18729.         PK_COLUMN_PROPID    = convert(int,null),
  18730.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18731.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18732.         FK_TABLE_NAME         = o2.name,
  18733.         FK_COLUMN_NAME         = c2.name,
  18734.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18735.         FK_COLUMN_PROPID    = convert(int,null),
  18736.         ORDINAL                = convert(int,12),
  18737.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18738.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18739.         PK_NAME                = i.name,
  18740.         FK_NAME                = object_name(r.constid),
  18741.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18742.     from
  18743.         sysobjects o1, sysobjects o2,
  18744.         syscolumns c1, syscolumns c2,
  18745.         sysreferences r, sysindexes i
  18746.     where    
  18747.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18748.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18749.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18750.     and    o1.id = r.rkeyid
  18751.     and    r.keycnt >= 12
  18752.     and    o1.id = c1.id
  18753.     and    c1.colid = r.rkey12
  18754.     and r.fkeyid = o2.id
  18755.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18756.     and    o2.id = c2.id    
  18757.     and    c2.colid = r.fkey12
  18758.     and permissions(o1.id) <> 0
  18759.     and permissions(o2.id) <> 0
  18760.     and i.id = r.rkeyid
  18761.     and i.indid = r.rkeyindid        
  18762.     union all
  18763.     select
  18764.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18765.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18766.         PK_TABLE_NAME         = o1.name,
  18767.         PK_COLUMN_NAME         = c1.name,
  18768.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18769.         PK_COLUMN_PROPID    = convert(int,null),
  18770.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18771.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18772.         FK_TABLE_NAME         = o2.name,
  18773.         FK_COLUMN_NAME         = c2.name,
  18774.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18775.         FK_COLUMN_PROPID    = convert(int,null),
  18776.         ORDINAL                = convert(int,13),
  18777.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18778.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18779.         PK_NAME                = i.name,
  18780.         FK_NAME                = object_name(r.constid),
  18781.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18782.     from
  18783.         sysobjects o1, sysobjects o2,
  18784.         syscolumns c1, syscolumns c2,
  18785.         sysreferences r, sysindexes i
  18786.     where    
  18787.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18788.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18789.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18790.     and    o1.id = r.rkeyid
  18791.     and    r.keycnt >= 13
  18792.     and    o1.id = c1.id
  18793.     and    c1.colid = r.rkey13
  18794.     and r.fkeyid = o2.id
  18795.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18796.     and    o2.id = c2.id    
  18797.     and    c2.colid = r.fkey13
  18798.     and permissions(o1.id) <> 0
  18799.     and permissions(o2.id) <> 0
  18800.     and i.id = r.rkeyid
  18801.     and i.indid = r.rkeyindid        
  18802.     union all
  18803.     select
  18804.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18805.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18806.         PK_TABLE_NAME         = o1.name,
  18807.         PK_COLUMN_NAME         = c1.name,
  18808.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18809.         PK_COLUMN_PROPID    = convert(int,null),
  18810.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18811.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18812.         FK_TABLE_NAME         = o2.name,
  18813.         FK_COLUMN_NAME         = c2.name,
  18814.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18815.         FK_COLUMN_PROPID    = convert(int,null),
  18816.         ORDINAL                = convert(int,14),
  18817.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18818.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18819.         PK_NAME                = i.name,
  18820.         FK_NAME                = object_name(r.constid),
  18821.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18822.     from
  18823.         sysobjects o1, sysobjects o2,
  18824.         syscolumns c1, syscolumns c2,
  18825.         sysreferences r, sysindexes i
  18826.     where    
  18827.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18828.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18829.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18830.     and    o1.id = r.rkeyid
  18831.     and    r.keycnt >= 14
  18832.     and    o1.id = c1.id
  18833.     and    c1.colid = r.rkey14
  18834.     and r.fkeyid = o2.id
  18835.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18836.     and    o2.id = c2.id    
  18837.     and    c2.colid = r.fkey14
  18838.     and permissions(o1.id) <> 0
  18839.     and permissions(o2.id) <> 0
  18840.     and i.id = r.rkeyid
  18841.     and i.indid = r.rkeyindid        
  18842.     union all
  18843.     select
  18844.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18845.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18846.         PK_TABLE_NAME         = o1.name,
  18847.         PK_COLUMN_NAME         = c1.name,
  18848.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18849.         PK_COLUMN_PROPID    = convert(int,null),
  18850.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18851.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18852.         FK_TABLE_NAME         = o2.name,
  18853.         FK_COLUMN_NAME         = c2.name,
  18854.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18855.         FK_COLUMN_PROPID    = convert(int,null),
  18856.         ORDINAL                = convert(int,15),
  18857.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18858.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18859.         PK_NAME                = i.name,
  18860.         FK_NAME                = object_name(r.constid),
  18861.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18862.     from
  18863.         sysobjects o1, sysobjects o2,
  18864.         syscolumns c1, syscolumns c2,
  18865.         sysreferences r, sysindexes i
  18866.     where    
  18867.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18868.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18869.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18870.     and    o1.id = r.rkeyid
  18871.     and    r.keycnt >= 15
  18872.     and    o1.id = c1.id
  18873.     and    c1.colid = r.rkey15
  18874.     and r.fkeyid = o2.id
  18875.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18876.     and    o2.id = c2.id    
  18877.     and    c2.colid = r.fkey15
  18878.     and permissions(o1.id) <> 0
  18879.     and permissions(o2.id) <> 0
  18880.     and i.id = r.rkeyid
  18881.     and i.indid = r.rkeyindid        
  18882.     union all
  18883.     select
  18884.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18885.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18886.         PK_TABLE_NAME         = o1.name,
  18887.         PK_COLUMN_NAME         = c1.name,
  18888.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18889.         PK_COLUMN_PROPID    = convert(int,null),
  18890.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18891.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18892.         FK_TABLE_NAME         = o2.name,
  18893.         FK_COLUMN_NAME         = c2.name,
  18894.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18895.         FK_COLUMN_PROPID    = convert(int,null),
  18896.         ORDINAL                = convert(int,16),
  18897.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18898.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18899.         PK_NAME                = i.name,
  18900.         FK_NAME                = object_name(r.constid),
  18901.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18902.     from
  18903.         sysobjects o1, sysobjects o2,
  18904.         syscolumns c1, syscolumns c2,
  18905.         sysreferences r, sysindexes i
  18906.     where    
  18907.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18908.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18909.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18910.     and    o1.id = r.rkeyid
  18911.     and    o1.id = c1.id
  18912.     and    r.keycnt >= 16
  18913.     and    c1.colid = r.rkey16
  18914.     and r.fkeyid = o2.id
  18915.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18916.     and    o2.id = c2.id    
  18917.     and    c2.colid = r.fkey16
  18918.     and permissions(o1.id) <> 0
  18919.     and permissions(o2.id) <> 0
  18920.     and i.id = r.rkeyid
  18921.     and i.indid = r.rkeyindid        
  18922.     order by 8,9,2,3,13
  18923. go
  18924. dump tran master with no_log
  18925. go
  18926. create procedure sp_foreign_keys_rowset;5
  18927.     (
  18928.     @server_name        sysname,
  18929.     @pk_catalog_name    sysname = null,
  18930.     @fk_catalog_name    sysname = null,
  18931.     @pk_table_name        sysname = null,
  18932.     @pk_table_schema    sysname = null,
  18933.     @fk_table_name        sysname = null,
  18934.     @fk_table_schema    sysname = null
  18935.     )
  18936. as
  18937.     select
  18938.         PK_TABLE_CATALOG,
  18939.         PK_TABLE_SCHEMA,    
  18940.         PK_TABLE_NAME,     
  18941.         PK_COLUMN_NAME,     
  18942.         PK_COLUMN_GUID,    
  18943.         PK_COLUMN_PROPID,
  18944.         FK_TABLE_CATALOG,
  18945.         FK_TABLE_SCHEMA, 
  18946.         FK_TABLE_NAME,     
  18947.         FK_COLUMN_NAME,     
  18948.         FK_COLUMN_GUID,    
  18949.         FK_COLUMN_PROPID,
  18950.         ORDINAL,        
  18951.         UPDATE_RULE,    
  18952.         DELETE_RULE
  18953.         --PK_NAME,        
  18954.         --FK_NAME,
  18955.         --DEFERRABILITY    
  18956.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  18957.                 @server_name,
  18958.                 @pk_catalog_name,
  18959.                 @pk_table_schema,
  18960.                 @pk_table_name,
  18961.                 @fk_catalog_name,
  18962.                 @fk_table_schema,
  18963.                 @fk_table_name >
  18964.     order by 7,8,9,1,2,3,13
  18965. go
  18966.  
  18967. grant execute on sp_foreign_keys_rowset to public
  18968. go
  18969.  
  18970. dump tran master with no_log
  18971. go
  18972. if (charindex('6.00', @@version) > 0)
  18973.     begin
  18974.     if (exists (select * from sysobjects
  18975.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  18976.         begin
  18977.         drop procedure sp_foreign_keys_rowset
  18978.         dump tran master with no_log
  18979.         end
  18980.     end
  18981. go
  18982.  
  18983.  
  18984. print ''
  18985. print 'creating sp_indexes_rowset'
  18986. go
  18987.  
  18988. /*    6.0 and 6.5 version */
  18989. create procedure sp_indexes_rowset
  18990.     (
  18991.     @table_name    varchar(255), 
  18992.     @index_name    varchar(255) = null,
  18993.     @table_schema    varchar(255) = null     
  18994.     )
  18995. as
  18996.     select    TABLE_CATALOG        = db_name(),
  18997.         TABLE_SCHEMA        = user_name(o.uid),
  18998.         TABLE_NAME        = o.name,
  18999.         INDEX_CATALOG        = db_name(),        
  19000.         INDEX_SCHEMA        = user_name(o.uid),
  19001.         INDEX_NAME        = x.name,
  19002.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19003.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19004.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19005.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19006.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19007.         INITIAL_SIZE        = convert(int,null),
  19008.         NULLS            = convert(int,null),
  19009.         SORT_BOOKMARKS        = convert(bit,0),
  19010.         AUTO_UPDATE        = convert(bit,1),
  19011.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19012.         ORDINAL_POSITION     = convert(int,
  19013.                        (
  19014.                         select count(*)
  19015.                         from syscolumns sc
  19016.                         where sc.id     =  c.id
  19017.                           AND sc.number =  c.number
  19018.                           AND sc.colid  <= c.colid
  19019.                         )),
  19020.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19021.         COLUMN_GUID        = convert(binary(16),null),
  19022.         COLUMN_PROPID        = convert(int,null),
  19023.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19024.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19025.         PAGES            = convert(int, x.dpages),
  19026.         FILTER_CONDITION    = convert(varchar(1),null),
  19027.         INTEGRATED        = convert(bit,(x.status & 16)/16)
  19028.         
  19029.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19030.     where    o.type in ('U')
  19031.     and     o.name = @table_name
  19032.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19033.     and    (@index_name is null or @index_name = x.name)
  19034.     and     x.id = o.id
  19035.     and     x.id = c.id
  19036.     and      c.colid < x.keycnt+(x.status&16)/16
  19037.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19038.     and     (
  19039.         suser_id() = 1     /* User is the System Administrator */
  19040.         or o.uid = user_id()     /* User created the object */
  19041.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19042.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19043.             from sysprotects p
  19044.             where p.id = o.id
  19045.             /* get rows for public,current user,user's group */
  19046.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19047.             /* check for SELECT,EXECUTE privilege */
  19048.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19049.             ) = 1    /* final magic...compare Grants      */
  19050.            )
  19051.     order by 8 desc, 4, 5, 6, 17
  19052. go
  19053. dump tran master with no_log
  19054. go
  19055. create procedure sp_indexes_rowset;2
  19056.     (
  19057.     @index_name    varchar(255) = null,
  19058.     @table_schema    varchar(255) = null     
  19059.     )
  19060. as
  19061.     select    TABLE_CATALOG        = db_name(),
  19062.         TABLE_SCHEMA        = user_name(o.uid),
  19063.         TABLE_NAME        = o.name,
  19064.         INDEX_CATALOG        = db_name(),        
  19065.         INDEX_SCHEMA        = user_name(o.uid),
  19066.         INDEX_NAME        = x.name,
  19067.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19068.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19069.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19070.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19071.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19072.         INITIAL_SIZE        = convert(int,null),
  19073.         NULLS            = convert(int,null),
  19074.         SORT_BOOKMARKS        = convert(bit,0),
  19075.         AUTO_UPDATE        = convert(bit,1),
  19076.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19077.         ORDINAL_POSITION     = convert(int,
  19078.                        (
  19079.                         select count(*)
  19080.                         from syscolumns sc
  19081.                         where sc.id     =  c.id
  19082.                           AND sc.number =  c.number
  19083.                           AND sc.colid  <= c.colid
  19084.                         )),
  19085.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19086.         COLUMN_GUID        = convert(binary(16),null),
  19087.         COLUMN_PROPID        = convert(int,null),
  19088.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19089.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19090.         PAGES            = convert(int, x.dpages),
  19091.         FILTER_CONDITION    = convert(varchar(1),null),
  19092.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19093.         
  19094.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19095.     where    o.type in ('U')
  19096.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19097.     and    (@index_name is null or @index_name = x.name)
  19098.     and     x.id = o.id
  19099.     and     x.id = c.id
  19100.     and      c.colid < x.keycnt+(x.status&16)/16
  19101.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19102.     and     (
  19103.         suser_id() = 1     /* User is the System Administrator */
  19104.         or o.uid = user_id()     /* User created the object */
  19105.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19106.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19107.             from sysprotects p
  19108.             where p.id = o.id
  19109.             /* get rows for public,current user,user's group */
  19110.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19111.             /* check for SELECT,EXECUTE privilege */
  19112.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19113.             ) = 1    /* final magic...compare Grants      */
  19114.            )
  19115.     order by 8 desc, 4, 5, 6, 17
  19116. go
  19117. dump tran master with no_log
  19118. go
  19119.  
  19120. if (charindex('7.00', @@version) > 0 or
  19121.     charindex('8.00', @@version) > 0)
  19122.     drop procedure sp_indexes_rowset
  19123. else
  19124. begin
  19125.     print ''
  19126.     print ''
  19127.     print 'Warning:'
  19128.     print 'you are installing the stored procedures '
  19129.     print 'on a pre 7.0 SQL Server.'
  19130.     print 'Ignore the following errors.'
  19131. end
  19132. go
  19133.  
  19134. /*    7.0 version */
  19135. create procedure sp_indexes_rowset
  19136.     (
  19137.     @table_name    sysname, 
  19138.     @index_name    sysname = null,
  19139.     @table_schema    sysname = null     
  19140.     )
  19141. as
  19142.     select    TABLE_CATALOG        = db_name(),
  19143.         TABLE_SCHEMA        = user_name(o.uid),
  19144.         TABLE_NAME        = o.name,
  19145.         INDEX_CATALOG        = db_name(),        
  19146.         INDEX_SCHEMA        = user_name(o.uid),
  19147.         INDEX_NAME        = x.name,
  19148.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19149.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19150.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19151.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19152.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19153.         INITIAL_SIZE        = convert(int,null),
  19154.         NULLS            = convert(int,null),
  19155.         SORT_BOOKMARKS        = convert(bit,0),
  19156.         AUTO_UPDATE        = convert(bit,1),
  19157.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19158.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19159.         COLUMN_NAME        = c.name,
  19160.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19161.         COLUMN_PROPID        = convert(int,null),
  19162.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19163.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19164.         PAGES            = convert(int, x.dpages),
  19165.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19166.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19167.         
  19168.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19169.     where    o.type in ('U')
  19170.     and     o.name = @table_name
  19171.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19172.     and    (@index_name is null or @index_name = x.name)
  19173.     and    x.id = o.id
  19174.     and    o.id = c.id
  19175.     and    o.id = xk.id
  19176.     and    x.indid = xk.indid
  19177.     and    c.colid = xk.colid
  19178.     and    xk.keyno <= x.keycnt
  19179.     and    permissions(o.id, c.name) <> 0
  19180.     and     (x.status&32) = 0  -- No hypothetical indexes
  19181.     order by 8 desc, 4, 5, 6, 17
  19182. go
  19183. dump tran master with no_log
  19184. go
  19185. create procedure sp_indexes_rowset;2
  19186.     (
  19187.     @index_name    sysname = null,
  19188.     @table_schema    sysname = null     
  19189.     )
  19190. as
  19191.     select    TABLE_CATALOG        = db_name(),
  19192.         TABLE_SCHEMA        = user_name(o.uid),
  19193.         TABLE_NAME        = o.name,
  19194.         INDEX_CATALOG        = db_name(),        
  19195.         INDEX_SCHEMA        = user_name(o.uid),
  19196.         INDEX_NAME        = x.name,
  19197.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19198.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19199.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19200.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19201.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19202.         INITIAL_SIZE        = convert(int,null),
  19203.         NULLS            = convert(int,null),
  19204.         SORT_BOOKMARKS        = convert(bit,0),
  19205.         AUTO_UPDATE        = convert(bit,1),
  19206.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19207.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19208.         COLUMN_NAME        = c.name,
  19209.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19210.         COLUMN_PROPID        = convert(int,null),
  19211.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19212.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19213.         PAGES            = convert(int, x.dpages),
  19214.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19215.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19216.         
  19217.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19218.     where    o.type in ('U')
  19219.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19220.     and    (@index_name is null or @index_name = x.name)
  19221.     and    x.id = o.id
  19222.     and    o.id = c.id
  19223.     and    o.id = xk.id
  19224.     and    x.indid = xk.indid
  19225.     and    c.colid = xk.colid
  19226.     and    xk.keyno <= x.keycnt
  19227.     and    permissions(o.id, c.name) <> 0
  19228.     and    (x.status&32) = 0  -- No hypothetical indexes
  19229.     order by 8 desc, 4, 5, 6, 17
  19230. go
  19231. dump tran master with no_log
  19232. go
  19233. create procedure sp_indexes_rowset;5
  19234.     (
  19235.     @table_server        sysname,
  19236.     @table_catalog        sysname = null,
  19237.     @table_name        sysname = null,
  19238.     @index_name        sysname = null,
  19239.     @table_schema        sysname = null
  19240.     )
  19241. as
  19242.     select
  19243.         TABLE_CATALOG,    
  19244.         TABLE_SCHEMA,    
  19245.         TABLE_NAME,    
  19246.         INDEX_CATALOG,        
  19247.         INDEX_SCHEMA,    
  19248.         INDEX_NAME,    
  19249.         PRIMARY_KEY,    
  19250.         "UNIQUE",    
  19251.         "CLUSTERED",    
  19252.         "TYPE",        
  19253.         FILL_FACTOR,    
  19254.         INITIAL_SIZE,    
  19255.         NULLS,        
  19256.         SORT_BOOKMARKS,    
  19257.         AUTO_UPDATE,    
  19258.         NULL_COLLATION,    
  19259.         ORDINAL_POSITION,
  19260.         COLUMN_NAME,    
  19261.         COLUMN_GUID,    
  19262.         COLUMN_PROPID,    
  19263.         COLLATION,    
  19264.         CARDINALITY,    
  19265.         PAGES,        
  19266.         FILTER_CONDITION
  19267.     --    INTEGRATED
  19268.     from master.dbo.SYSREMOTE_INDEXES <
  19269.                 @table_server,
  19270.                 @table_catalog,
  19271.                 @table_schema,
  19272.                 @index_name,
  19273.                 NULL,            /* TYPE (index type) */
  19274.                 @table_name >
  19275.     order by 8 desc, 4, 5, 6, 17
  19276. go
  19277.  
  19278. grant execute on sp_indexes_rowset to public
  19279. go
  19280.  
  19281. dump tran master with no_log
  19282. go
  19283.  
  19284. if (charindex('8.00', @@version) > 0)
  19285.     drop procedure sp_indexes_rowset
  19286. else
  19287. begin
  19288.     print ''
  19289.     print ''
  19290.     print 'Warning:'
  19291.     print 'you are installing the stored procedures '
  19292.     print 'on a pre 8.0 SQL Server.'
  19293.     print 'Ignore the following errors.'
  19294. end
  19295. go
  19296.  
  19297. /*    8.0 version */
  19298. create procedure sp_indexes_rowset
  19299.     (
  19300.     @table_name    sysname, 
  19301.     @index_name    sysname = null,
  19302.     @table_schema    sysname = null     
  19303.     )
  19304. as
  19305.     select    TABLE_CATALOG        = db_name(),
  19306.         TABLE_SCHEMA        = user_name(o.uid),
  19307.         TABLE_NAME        = o.name,
  19308.         INDEX_CATALOG        = db_name(),        
  19309.         INDEX_SCHEMA        = user_name(o.uid),
  19310.         INDEX_NAME        = x.name,
  19311.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19312.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19313.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19314.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19315.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19316.         INITIAL_SIZE        = convert(int,null),
  19317.         NULLS            = convert(int,null),
  19318.         SORT_BOOKMARKS        = convert(bit,0),
  19319.         AUTO_UPDATE        = convert(bit,1),
  19320.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19321.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19322.         COLUMN_NAME        = c.name,
  19323.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19324.         COLUMN_PROPID        = convert(int,null),
  19325.         COLLATION    = convert(smallint,
  19326.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19327.                         then 2        /* DB_COLLATION_DESC */ 
  19328.                         else 1        /* DB_COLLATION_ASC */ 
  19329.                         end),
  19330.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19331.         PAGES            = convert(int, x.dpages),
  19332.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19333.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19334.         
  19335.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19336.     where    o.type in ('U')
  19337.     and     o.name = @table_name
  19338.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19339.     and    (@index_name is null or @index_name = x.name)
  19340.     and    x.id = o.id
  19341.     and    o.id = c.id
  19342.     and    o.id = xk.id
  19343.     and    x.indid = xk.indid
  19344.     and    c.colid = xk.colid
  19345.     and    xk.keyno <= x.keycnt
  19346.     and    permissions(o.id, c.name) <> 0
  19347.     and     (x.status&32) = 0  -- No hypothetical indexes
  19348.     order by 8 desc, 4, 5, 6, 17
  19349. go
  19350. dump tran master with no_log
  19351. go
  19352. create procedure sp_indexes_rowset;2
  19353.     (
  19354.     @index_name    sysname = null,
  19355.     @table_schema    sysname = null     
  19356.     )
  19357. as
  19358.     select    TABLE_CATALOG        = db_name(),
  19359.         TABLE_SCHEMA        = user_name(o.uid),
  19360.         TABLE_NAME        = o.name,
  19361.         INDEX_CATALOG        = db_name(),        
  19362.         INDEX_SCHEMA        = user_name(o.uid),
  19363.         INDEX_NAME        = x.name,
  19364.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19365.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19366.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19367.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19368.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19369.         INITIAL_SIZE        = convert(int,null),
  19370.         NULLS            = convert(int,null),
  19371.         SORT_BOOKMARKS        = convert(bit,0),
  19372.         AUTO_UPDATE        = convert(bit,1),
  19373.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19374.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19375.         COLUMN_NAME        = c.name,
  19376.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19377.         COLUMN_PROPID        = convert(int,null),
  19378.         COLLATION    = convert(smallint,
  19379.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19380.                         then 2        /* DB_COLLATION_DESC */ 
  19381.                         else 1        /* DB_COLLATION_ASC */ 
  19382.                         end),
  19383.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19384.         PAGES            = convert(int, x.dpages),
  19385.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19386.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19387.         
  19388.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19389.     where    o.type in ('U')
  19390.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19391.     and    (@index_name is null or @index_name = x.name)
  19392.     and    x.id = o.id
  19393.     and    o.id = c.id
  19394.     and    o.id = xk.id
  19395.     and    x.indid = xk.indid
  19396.     and    c.colid = xk.colid
  19397.     and    xk.keyno <= x.keycnt
  19398.     and    permissions(o.id, c.name) <> 0
  19399.     and    (x.status&32) = 0  -- No hypothetical indexes
  19400.     order by 8 desc, 4, 5, 6, 17
  19401. go
  19402. dump tran master with no_log
  19403. go
  19404. create procedure sp_indexes_rowset;5
  19405.     (
  19406.     @table_server        sysname,
  19407.     @table_catalog        sysname = null,
  19408.     @table_name        sysname = null,
  19409.     @index_name        sysname = null,
  19410.     @table_schema        sysname = null
  19411.     )
  19412. as
  19413.     select
  19414.         TABLE_CATALOG,    
  19415.         TABLE_SCHEMA,    
  19416.         TABLE_NAME,    
  19417.         INDEX_CATALOG,        
  19418.         INDEX_SCHEMA,    
  19419.         INDEX_NAME,    
  19420.         PRIMARY_KEY,    
  19421.         "UNIQUE",    
  19422.         "CLUSTERED",    
  19423.         "TYPE",        
  19424.         FILL_FACTOR,    
  19425.         INITIAL_SIZE,    
  19426.         NULLS,        
  19427.         SORT_BOOKMARKS,    
  19428.         AUTO_UPDATE,    
  19429.         NULL_COLLATION,    
  19430.         ORDINAL_POSITION,
  19431.         COLUMN_NAME,    
  19432.         COLUMN_GUID,    
  19433.         COLUMN_PROPID,    
  19434.         COLLATION,    
  19435.         CARDINALITY,    
  19436.         PAGES,        
  19437.         FILTER_CONDITION
  19438.     --    INTEGRATED
  19439.     from master.dbo.SYSREMOTE_INDEXES <
  19440.                 @table_server,
  19441.                 @table_catalog,
  19442.                 @table_schema,
  19443.                 @index_name,
  19444.                 NULL,            /* TYPE (index type) */
  19445.                 @table_name >
  19446.     order by 8 desc, 4, 5, 6, 17
  19447. go
  19448.  
  19449. grant execute on sp_indexes_rowset to public
  19450. go
  19451.  
  19452. dump tran master with no_log
  19453. go
  19454. if (charindex('6.00', @@version) > 0)
  19455.     begin
  19456.     if (exists (select * from sysobjects
  19457.             where name = 'sp_indexes_rowset' and type = 'P '))
  19458.         begin
  19459.         drop procedure sp_indexes_rowset
  19460.         dump tran master with no_log
  19461.         end
  19462.     end
  19463. go
  19464.  
  19465.  
  19466. print ''
  19467. print 'creating sp_primary_keys_rowset'
  19468. go
  19469.  
  19470. /*    Procedure for 6.0 and 6.5 servers */
  19471. create procedure sp_primary_keys_rowset
  19472.     (
  19473.        @table_name        varchar(255),
  19474.     @table_schema         varchar(244) = null
  19475.     )
  19476. as
  19477.     select    TABLE_CATALOG    = db_name(),
  19478.             TABLE_SCHEMA    = user_name(o.uid),
  19479.             TABLE_NAME        = o.name,    
  19480.             COLUMN_NAME        = c.name,
  19481.             COLUMN_GUID        = convert(binary(16),null),
  19482.             COLUMN_PROPID    = convert(int,null),
  19483.             ORDINAL            = convert(int,c1.colid),
  19484.             PK_NAME            = i.name
  19485.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  19486.     where    o.type in ('U')
  19487.     and     o.name = @table_name
  19488.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19489.     and     o.id = c.id
  19490.     and     o.id = i.id
  19491.     and     (i.status & 0x800) = 0x800
  19492.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19493.     and     c1.id = c.id
  19494.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19495.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19496.     and     (
  19497.         suser_id() = 1     /* User is the System Administrator */
  19498.         or o.uid = user_id()     /* User created the object */
  19499.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19500.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19501.             from sysprotects p
  19502.             where p.id = o.id
  19503.             /* get rows for public,current user,user's group */
  19504.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19505.             /* check for SELECT,EXECUTE privilege */
  19506.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19507.             ) = 1    /* final magic...compare Grants      */
  19508.            )
  19509.     order by 2, 3
  19510. go
  19511. dump tran master with no_log
  19512. go
  19513. create procedure sp_primary_keys_rowset;2
  19514.     (
  19515.      @table_schema         varchar(244) = null
  19516.     )
  19517. as
  19518.     select    TABLE_CATALOG    = db_name(),
  19519.             TABLE_SCHEMA    = user_name(o.uid),
  19520.             TABLE_NAME        = o.name,    
  19521.             COLUMN_NAME        = c.name,
  19522.             COLUMN_GUID        = convert(binary(16),null),
  19523.             COLUMN_PROPID    = convert(int,null),
  19524.             ORDINAL            = convert(int,c1.colid),
  19525.             PK_NAME            = i.name
  19526.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  19527.     where    o.type in ('U')
  19528.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19529.     and     o.id = c.id
  19530.     and     o.id = i.id
  19531.     and     (i.status & 0x800) = 0x800
  19532.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19533.     and     c1.id = c.id
  19534.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19535.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19536.     and     (
  19537.         suser_id() = 1     /* User is the System Administrator */
  19538.         or o.uid = user_id()     /* User created the object */
  19539.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19540.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19541.             from sysprotects p
  19542.             where p.id = o.id
  19543.             /* get rows for public,current user,user's group */
  19544.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19545.             /* check for SELECT,EXECUTE privilege */
  19546.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19547.             ) = 1    /* final magic...compare Grants      */
  19548.            )
  19549.     order by 2, 3
  19550. go
  19551.  
  19552. if (charindex('7.00', @@version) = 0 and
  19553.     charindex('8.00', @@version) = 0)
  19554. begin
  19555.     print ''
  19556.     print ''
  19557.     print 'Warning:'
  19558.     print 'you are installing the stored procedures '
  19559.     print 'on a pre 8.0 SQL Server.'
  19560.     print 'Ignore the following errors.'
  19561. end
  19562. else
  19563.     drop proc sp_primary_keys_rowset
  19564. go
  19565.  
  19566. /*    Procedure for 8.0 servers */
  19567. create procedure sp_primary_keys_rowset
  19568.     (
  19569.        @table_name        sysname,
  19570.     @table_schema         sysname = null
  19571.     )
  19572. as
  19573.     select    TABLE_CATALOG    = db_name(),
  19574.             TABLE_SCHEMA    = user_name(o.uid),
  19575.             TABLE_NAME        = o.name,    
  19576.             COLUMN_NAME        = c.name,
  19577.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19578.             COLUMN_PROPID    = convert(int,null),
  19579.             ORDINAL            = convert(int,c1.colid),
  19580.             PK_NAME            = i.name
  19581.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19582.     where    o.type in ('U')
  19583.     and     o.name = @table_name
  19584.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19585.     and     o.id = c.id
  19586.     and     o.id = i.id
  19587.     and     (i.status & 0x800) = 0x800
  19588.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19589.     and     c1.id = c.id
  19590.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19591.     and    permissions(o.id) <> 0
  19592.     order by 2, 3
  19593. go
  19594. dump tran master with no_log
  19595. go
  19596. create procedure sp_primary_keys_rowset;2
  19597.     (
  19598.      @table_schema     sysname = null
  19599.     )
  19600. as
  19601.     select    TABLE_CATALOG    = db_name(),
  19602.             TABLE_SCHEMA    = user_name(o.uid),
  19603.             TABLE_NAME        = o.name,    
  19604.             COLUMN_NAME        = c.name,
  19605.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19606.             COLUMN_PROPID    = convert(int,null),
  19607.             ORDINAL            = convert(int,c1.colid),
  19608.             PK_NAME            = i.name
  19609.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19610.     where    o.type in ('U')
  19611.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19612.     and     o.id = c.id
  19613.     and     o.id = i.id
  19614.     and     (i.status & 0x800) = 0x800
  19615.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19616.     and     c1.id = c.id
  19617.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19618.     and    permissions(o.id) <> 0
  19619.     order by 2, 3
  19620. go
  19621. dump tran master with no_log
  19622. go
  19623. create procedure sp_primary_keys_rowset;3
  19624.     (
  19625.        @table_name        sysname = null,
  19626.     @table_schema         sysname = null
  19627.     )
  19628. as
  19629. IF @table_name is not NULL
  19630.     BEGIN
  19631.     select    TABLE_CATALOG    = db_name(),
  19632.             TABLE_SCHEMA    = user_name(o.uid),
  19633.             TABLE_NAME        = o.name,    
  19634.             COLUMN_NAME        = c.name,
  19635.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19636.             COLUMN_PROPID    = convert(int,null),
  19637.             ORDINAL            = convert(int,c1.colid),
  19638.             PK_NAME            = i.name
  19639.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19640.     where    o.type in ('U')
  19641.     and     o.name = @table_name
  19642.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19643.     and     o.id = c.id
  19644.     and     o.id = i.id
  19645.     and     (i.status & 0x800) = 0x800
  19646.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19647.     and     c1.id = c.id
  19648.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19649.     and    permissions(o.id) <> 0
  19650.     END
  19651. ELSE
  19652.     BEGIN
  19653.     select    TABLE_CATALOG    = db_name(),
  19654.             TABLE_SCHEMA    = user_name(o.uid),
  19655.             TABLE_NAME        = o.name,    
  19656.             COLUMN_NAME        = c.name,
  19657.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19658.             COLUMN_PROPID    = convert(int,null),
  19659.             ORDINAL            = convert(int,c1.colid),
  19660.             PK_NAME            = i.name
  19661.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19662.     where    o.type in ('U')
  19663.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19664.     and     o.id = c.id
  19665.     and     o.id = i.id
  19666.     and     (i.status & 0x800) = 0x800
  19667.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19668.     and     c1.id = c.id
  19669.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19670.     and    permissions(o.id) <> 0
  19671.     END
  19672. go
  19673. dump tran master with no_log
  19674. go
  19675. create procedure sp_primary_keys_rowset;5
  19676.     (
  19677.     @table_server        sysname,
  19678.     @table_catalog        sysname = null,
  19679.     @table_name            sysname = null,
  19680.     @table_schema        sysname = null
  19681.     )
  19682. as
  19683.     select
  19684.         TABLE_CATALOG,    
  19685.         TABLE_SCHEMA,    
  19686.         TABLE_NAME,    
  19687.         COLUMN_NAME,    
  19688.         COLUMN_GUID,    
  19689.         COLUMN_PROPID,    
  19690.         ORDINAL
  19691.         --PK_NAME        
  19692.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  19693.                 @table_server,
  19694.                 @table_catalog,
  19695.                 @table_schema,
  19696.                 @table_name >
  19697.     order by 1,2,3
  19698. go
  19699.  
  19700. grant execute on sp_primary_keys_rowset to public
  19701. go
  19702.  
  19703. dump tran master with no_log
  19704. go
  19705. if (charindex('6.00', @@version) > 0)
  19706.     begin
  19707.     if (exists (select * from sysobjects
  19708.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  19709.         begin
  19710.         drop procedure sp_primary_keys_rowset
  19711.         dump tran master with no_log
  19712.         end
  19713.     end
  19714. go
  19715.  
  19716.  
  19717. print ''
  19718. print 'creating sp_provider_types_rowset'
  19719. go
  19720.  
  19721. /*    Procedure for 6.0 and 6.50 servers */
  19722. create proc sp_provider_types_rowset
  19723.     (
  19724.     @data_type     smallint = null,
  19725.     @best_match    tinyint  = null
  19726.     )
  19727. as
  19728.     select
  19729.         TYPE_NAME         = case when t.usertype = 80 then t.name 
  19730.                         else d.type_name 
  19731.                         end,
  19732.         DATA_TYPE         = d.oledb_data_type,                            
  19733.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19734.                             then @@max_precision
  19735.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  19736.         LITERAL_PREFIX         = d.literal_prefix,
  19737.         LITERAL_SUFFIX         = d.literal_suffix,
  19738.         CREATE_PARAMS         = convert(varchar(32),e.CREATE_PARAMS),
  19739.         IS_NULLABLE        = t.allownulls,
  19740.         CASE_SENSITIVE        = d.case_sensitive,
  19741.         SEARCHABLE         = d.searchable,
  19742.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  19743.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  19744.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  19745.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  19746.                         else d.local_type_name
  19747.                         end,
  19748.         MINIMUM_SCALE        = convert(smallint,
  19749.                         case 
  19750.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  19751.                         else null 
  19752.                         end),
  19753.         MAXIMUM_SCALE        = convert(smallint,
  19754.                         case 
  19755.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19756.                         then @@max_precision
  19757.                         else null 
  19758.                         end),
  19759.         GUID            = convert(binary(16),null),
  19760.         TYPELIB            = convert(varchar(1),null),
  19761.         VERSION            = convert(varchar(1),null),
  19762.         IS_LONG            = d.is_long,
  19763.         BEST_MATCH        = case when t.usertype = 80
  19764.                         then convert(bit,0)
  19765.                         else d.best_match
  19766.                         end,
  19767.         IS_FIXEDLENGTH        = convert(bit, 
  19768.                         case when d.fixlen is null then 0 else 1 end)
  19769.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  19770.     where    d.ss_dtype = t.type 
  19771.     and    t.usertype <= 100
  19772.     and    t.usertype <> 18 /* sysname */
  19773.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  19774.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  19775.     and     t.usertype *= e.user_type 
  19776.     and     e.AUTO_INCREMENT = 0
  19777.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  19778.     and    (@data_type is null or d.oledb_data_type = @data_type)
  19779.     and    (@best_match is null or d.best_match = @best_match)
  19780.     order by 2
  19781. go
  19782. dump tran master with no_log
  19783. go
  19784.  
  19785. if (charindex('7.00', @@version) = 0 and
  19786.     charindex('8.00', @@version) = 0)
  19787. begin
  19788.     print ''
  19789.     print ''
  19790.     print 'Warning:'
  19791.     print 'you are installing the stored procedures '
  19792.     print 'on a pre 8.0 SQL Server.'
  19793.     print 'Ignore the following errors.'
  19794. end
  19795. else
  19796.     drop proc sp_provider_types_rowset
  19797. go
  19798.  
  19799. /*    Procedure for 8.0 server */
  19800. create proc sp_provider_types_rowset
  19801.     (
  19802.     @data_type     smallint = null,
  19803.     @best_match    tinyint  = null
  19804.     )
  19805. as
  19806.     select
  19807.         TYPE_NAME         = case    when t.usertype = 80 then t.name 
  19808.                         else d.type_name 
  19809.                         end,
  19810.         DATA_TYPE         = d.oledb_data_type,                            
  19811.  
  19812.  
  19813.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19814.                             then @@max_precision
  19815.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  19816.         LITERAL_PREFIX         = d.literal_prefix,
  19817.         LITERAL_SUFFIX         = d.literal_suffix,
  19818.         CREATE_PARAMS         = convert(nvarchar(32),e.CREATE_PARAMS),
  19819.         IS_NULLABLE        = t.allownulls,
  19820.         CASE_SENSITIVE        = d.case_sensitive,
  19821.         SEARCHABLE         = d.searchable,
  19822.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  19823.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  19824.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  19825.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  19826.                         else d.local_type_name
  19827.                         end,
  19828.         MINIMUM_SCALE        = convert(smallint,
  19829.                         case 
  19830.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  19831.                         else null 
  19832.                         end),
  19833.         MAXIMUM_SCALE        = convert(smallint,
  19834.                         case 
  19835.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19836.                         then @@max_precision
  19837.                         else null 
  19838.                         end),
  19839.         GUID            = convert(uniqueidentifier,null),
  19840.         TYPELIB            = convert(nvarchar(1),null),
  19841.         VERSION            = convert(nvarchar(1),null),
  19842.         IS_LONG            = d.is_long,
  19843.         BEST_MATCH        = case    when t.usertype = 80
  19844.                         then convert(bit,0)
  19845.                         else d.best_match
  19846.                         end,
  19847.         IS_FIXEDLENGTH        = convert(bit, 
  19848.                         case when d.fixlen is null then 0 else 1 end)
  19849.  
  19850.     from master.dbo.spt_provider_types d
  19851.         INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
  19852.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  19853.             t.xusertype = e.user_type
  19854.             and e.AUTO_INCREMENT = 0
  19855.     where    (@data_type is null or d.oledb_data_type = @data_type)
  19856.     and    (@best_match is null or d.best_match = @best_match)    
  19857.     and    t.usertype <= 255
  19858.     and    t.usertype <> 18 /* sysname */
  19859.     order by 2
  19860. go
  19861.  
  19862. grant execute on sp_provider_types_rowset to public
  19863. go
  19864.  
  19865. dump tran master with no_log
  19866. go
  19867. if (charindex('6.00', @@version) > 0)
  19868.     begin
  19869.     if (exists (select * from sysobjects
  19870.         where name = 'sp_provider_types_rowset' and type = 'P '))
  19871.         begin
  19872.         drop procedure sp_provider_types_rowset
  19873.         dump tran master with no_log
  19874.         end
  19875.     end
  19876. go
  19877.  
  19878.  
  19879. print ''
  19880. print 'creating sp_procedure_params_rowset'
  19881. go
  19882.  
  19883. /*    Procedure for 6.0 and 6.50 servers */
  19884. create procedure sp_procedure_params_rowset
  19885.     (
  19886.        @procedure_name        varchar(255) = null,
  19887.     @group_number        int = null,
  19888.     @procedure_schema     varchar(255) = null,
  19889.     @parameter_name        varchar(255) = null
  19890.     )
  19891. as
  19892. IF @procedure_name is not null
  19893.     BEGIN
  19894.     select
  19895.         PROCEDURE_CATALOG     = db_name(),
  19896.         PROCEDURE_SCHEMA     = user_name(o.uid),
  19897.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  19898.         PARAMETER_NAME         = c.name,
  19899.         ORDINAL_POSITION     = convert(smallint, c.colid),
  19900.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  19901.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  19902.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  19903.         IS_NULLABLE        = convert(bit,
  19904.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  19905.                         then 0 else 1
  19906.                         end),
  19907.         DATA_TYPE        = d.oledb_data_type,
  19908.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  19909.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  19910.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  19911.                         then coalesce(d.column_size,c.length)
  19912.                         else null 
  19913.                         end),
  19914.         CHARACTER_OCTET_LENGTH    = convert(int,
  19915.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  19916.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  19917.                         then coalesce(d.column_size,c.length)
  19918.                         else null 
  19919.                         end),
  19920.         NUMERIC_PRECISION    = convert(smallint,
  19921.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
  19922.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  19923.                             then d.data_precision else null end),
  19924.         NUMERIC_SCALE        = convert(smallint, 
  19925.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  19926.         DESCRIPTION        = convert(varchar(1),null),
  19927.         TYPE_NAME        = d.type_name,
  19928.         LOCAL_TYPE_NAME        = d.local_type_name
  19929.     
  19930.     from
  19931.         syscolumns c,
  19932.         sysobjects o,
  19933.         master.dbo.spt_provider_types d,
  19934.         systypes t
  19935.     where
  19936.         o.name = @procedure_name
  19937.     and    o.type = 'P'                            /* Just Procedures */
  19938.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  19939.     and    o.id = c.id
  19940.     and    c.number = @group_number
  19941.     and    c.type = d.ss_dtype
  19942.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  19943.     and    c.usertype = t.usertype
  19944.     and    (t.usertype != 80 or d.type_name='timestamp')
  19945.     and    (@parameter_name is null or @parameter_name = c.name)
  19946.     UNION ALL
  19947.     SELECT           /* return value row*/
  19948.         PROCEDURE_CATALOG     = db_name(),
  19949.         PROCEDURE_SCHEMA     = user_name(o.uid),
  19950.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  19951.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  19952.         ORDINAL_POSITION     = convert(smallint,0),
  19953.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  19954.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  19955.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  19956.         IS_NULLABLE        = convert(bit,0),
  19957.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  19958.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  19959.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  19960.         NUMERIC_PRECISION    = convert(smallint,10),
  19961.         NUMERIC_SCALE        = convert(smallint,null),
  19962.         DESCRIPTION        = convert(varchar(1),null),
  19963.         TYPE_NAME        = convert(sysname,'int'),
  19964.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  19965.     from
  19966.         syscomments c,
  19967.         sysobjects o
  19968.     where
  19969.         o.name = @procedure_name
  19970.     and    o.type = 'P'                        /* Just Procedures */
  19971.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  19972.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  19973.     and    c.id = o.id
  19974.     and    c.number = @group_number
  19975.     and c.colid = 1
  19976.     order by 2, 3, 5
  19977.     END
  19978. ELSE
  19979.     BEGIN
  19980.     select
  19981.         PROCEDURE_CATALOG     = db_name(),
  19982.         PROCEDURE_SCHEMA     = user_name(o.uid),
  19983.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  19984.         PARAMETER_NAME         = c.name,
  19985.         ORDINAL_POSITION     = convert(smallint, c.colid),
  19986.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  19987.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  19988.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  19989.         IS_NULLABLE        = convert(bit,
  19990.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  19991.                         then 0 else 1
  19992.                         end),
  19993.         DATA_TYPE        = d.oledb_data_type,
  19994.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  19995.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  19996.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  19997.                         then coalesce(d.column_size,c.length)
  19998.                         else null 
  19999.                         end),
  20000.         CHARACTER_OCTET_LENGTH    = convert(int,
  20001.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20002.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20003.                         then coalesce(d.column_size,c.length)
  20004.                         else null 
  20005.                         end),
  20006.         NUMERIC_PRECISION    = convert(smallint,
  20007.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20008.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20009.                             then d.data_precision else null end),
  20010.         NUMERIC_SCALE        = convert(smallint, 
  20011.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20012.         DESCRIPTION        = convert(varchar(1),null),
  20013.         TYPE_NAME        = d.type_name,
  20014.         LOCAL_TYPE_NAME        = d.local_type_name
  20015.     
  20016.     from
  20017.         syscolumns c,
  20018.         sysobjects o,
  20019.         master.dbo.spt_provider_types d,
  20020.         systypes t
  20021.     where
  20022.         o.type = 'P'                            /* Just Procedures */
  20023.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20024.     and    o.id = c.id
  20025.     and    c.type = d.ss_dtype
  20026.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20027.     and    c.usertype = t.usertype
  20028.     and    (t.usertype != 80 or d.type_name='timestamp')
  20029.     and    (@parameter_name is null or @parameter_name = c.name)
  20030.     UNION ALL
  20031.     SELECT           /* return value row*/
  20032.         PROCEDURE_CATALOG     = db_name(),
  20033.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20034.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20035.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20036.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20037.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20038.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20039.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20040.         IS_NULLABLE        = convert(bit,0),
  20041.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20042.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20043.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20044.         NUMERIC_PRECISION    = convert(smallint,10),
  20045.         NUMERIC_SCALE        = convert(smallint,null),
  20046.         DESCRIPTION        = convert(varchar(1),null),
  20047.         TYPE_NAME        = convert(sysname,'int'),
  20048.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20049.     from
  20050.         syscomments c,
  20051.         sysobjects o
  20052.     where
  20053.         o.type = 'P'                        /* Just Procedures */
  20054.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20055.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20056.     and    c.id = o.id
  20057.     and     c.colid = 1
  20058.     order by 2, 3, 5
  20059.     END
  20060. go
  20061. dump tran master with no_log
  20062. go
  20063. create procedure sp_procedure_params_rowset;2
  20064.     (
  20065.       @handle            int output,
  20066.        @scrollopt        int output,
  20067.     @ccopt            int output,
  20068.     @rows            int output,
  20069.        @procedure_name        varchar(255) = null,
  20070.     @group_number        int = null,
  20071.     @procedure_schema     varchar(255) = null,
  20072.     @parameter_name        varchar(255) = null
  20073.     )
  20074. as
  20075.     declare @ret int
  20076.  
  20077.  
  20078. SET NOCOUNT ON
  20079.  
  20080.     create table #spprocparamrowset1
  20081.         (
  20082.         PROCEDURE_CATALOG     sysname not null,
  20083.         PROCEDURE_SCHEMA     sysname not null,
  20084.         PROCEDURE_NAME         varchar(35) not null,
  20085.         PARAMETER_NAME         sysname not null,
  20086.         ORDINAL_POSITION     smallint not null,
  20087.         PARAMETER_TYPE         smallint null,
  20088.         PARAMETER_HASDEFAULT    tinyint null,
  20089.         PARAMETER_DEFAULT    varchar(255) null,
  20090.         IS_NULLABLE        bit not null,
  20091.         DATA_TYPE        smallint null,
  20092.         CHARACTER_MAXIMUM_LENGTH int null,
  20093.         CHARACTER_OCTET_LENGTH    int null,
  20094.         NUMERIC_PRECISION    smallint null,
  20095.         NUMERIC_SCALE        smallint null,
  20096.         DESCRIPTION        varchar(1) null,
  20097.         TYPE_NAME        sysname null,
  20098.         LOCAL_TYPE_NAME        sysname null,
  20099.         )
  20100.  
  20101. IF @procedure_name is not null
  20102.     BEGIN
  20103.     insert into #spprocparamrowset1
  20104.     select
  20105.         PROCEDURE_CATALOG     = db_name(),
  20106.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20107.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20108.         PARAMETER_NAME         = c.name,
  20109.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20110.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  20111.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20112.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20113.         IS_NULLABLE        = convert(bit,
  20114.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20115.                         then 0 else 1
  20116.                         end),
  20117.         DATA_TYPE        = d.oledb_data_type,
  20118.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20119.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20120.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20121.                         then coalesce(d.column_size,c.length)
  20122.                         else null 
  20123.                         end),
  20124.         CHARACTER_OCTET_LENGTH    = convert(int,
  20125.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20126.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20127.                         then coalesce(d.column_size,c.length)
  20128.                         else null 
  20129.                         end),
  20130.         NUMERIC_PRECISION    = convert(smallint,
  20131.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20132.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20133.                             then d.data_precision else null end),
  20134.         NUMERIC_SCALE        = convert(smallint, 
  20135.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20136.         DESCRIPTION        = convert(varchar(1),null),
  20137.         TYPE_NAME        = d.type_name,
  20138.         LOCAL_TYPE_NAME        = d.local_type_name
  20139.     
  20140.     from
  20141.         syscolumns c,
  20142.         sysobjects o,
  20143.         master.dbo.spt_provider_types d,
  20144.         systypes t
  20145.     where
  20146.         o.name = @procedure_name
  20147.     and    o.type = 'P'                            /* Just Procedures */
  20148.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20149.     and    o.id = c.id
  20150.     and    c.number = @group_number
  20151.     and    c.type = d.ss_dtype
  20152.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20153.     and    c.usertype = t.usertype
  20154.     and    (t.usertype != 80 or d.type_name='timestamp')
  20155.     and    (@parameter_name is null or @parameter_name = c.name)
  20156.     UNION ALL
  20157.     SELECT           /* return value row*/
  20158.         PROCEDURE_CATALOG     = db_name(),
  20159.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20160.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20161.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20162.         ORDINAL_POSITION     = convert(smallint,0),
  20163.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20164.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20165.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20166.         IS_NULLABLE        = convert(bit,0),
  20167.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20168.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20169.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20170.         NUMERIC_PRECISION    = convert(smallint,10),
  20171.         NUMERIC_SCALE        = convert(smallint,null),
  20172.         DESCRIPTION        = convert(varchar(1),null),
  20173.         TYPE_NAME        = convert(sysname,'int'),
  20174.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20175.     from
  20176.         syscomments c,
  20177.         sysobjects o
  20178.     where
  20179.         o.name = @procedure_name
  20180.     and    o.type = 'P'                        /* Just Procedures */
  20181.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20182.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20183.     and    c.id = o.id
  20184.     and    c.number = @group_number
  20185.     and c.colid = 1
  20186.     order by 2, 3, 5
  20187.     END
  20188. ELSE
  20189.     BEGIN
  20190.     insert into #spprocparamrowset1
  20191.     select
  20192.         PROCEDURE_CATALOG     = db_name(),
  20193.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20194.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20195.         PARAMETER_NAME         = c.name,
  20196.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20197.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  20198.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20199.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20200.         IS_NULLABLE        = convert(bit,
  20201.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20202.                         then 0 else 1
  20203.                         end),
  20204.         DATA_TYPE        = d.oledb_data_type,
  20205.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20206.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20207.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20208.                         then coalesce(d.column_size,c.length)
  20209.                         else null 
  20210.                         end),
  20211.         CHARACTER_OCTET_LENGTH    = convert(int,
  20212.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20213.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20214.                         then coalesce(d.column_size,c.length)
  20215.                         else null 
  20216.                         end),
  20217.         NUMERIC_PRECISION    = convert(smallint,
  20218.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20219.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20220.                             then d.data_precision else null end),
  20221.         NUMERIC_SCALE        = convert(smallint, 
  20222.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20223.         DESCRIPTION        = convert(varchar(1),null),
  20224.         TYPE_NAME        = d.type_name,
  20225.         LOCAL_TYPE_NAME        = d.local_type_name
  20226.     
  20227.     from
  20228.         syscolumns c,
  20229.         sysobjects o,
  20230.         master.dbo.spt_provider_types d,
  20231.         systypes t
  20232.     where
  20233.         o.type = 'P'                            /* Just Procedures */
  20234.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20235.     and    o.id = c.id
  20236.     and    c.type = d.ss_dtype
  20237.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20238.     and    c.usertype = t.usertype
  20239.     and    (t.usertype != 80 or d.type_name='timestamp')
  20240.     and    (@parameter_name is null or @parameter_name = c.name)
  20241.     UNION ALL
  20242.     SELECT           /* return value row*/
  20243.         PROCEDURE_CATALOG     = db_name(),
  20244.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20245.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20246.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20247.         ORDINAL_POSITION     = convert(smallint,0),
  20248.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20249.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20250.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20251.         IS_NULLABLE        = convert(bit,0),
  20252.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20253.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20254.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20255.         NUMERIC_PRECISION    = convert(smallint,10),
  20256.         NUMERIC_SCALE        = convert(smallint,null),
  20257.         DESCRIPTION        = convert(varchar(1),null),
  20258.         TYPE_NAME        = convert(sysname,'int'),
  20259.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20260.     from
  20261.         syscomments c,
  20262.         sysobjects o
  20263.     where
  20264.         o.type = 'P'                        /* Just Procedures */
  20265.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20266.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20267.     and    c.id = o.id
  20268.     and     c.colid = 1
  20269.     order by 2, 3, 5
  20270.     END
  20271.  
  20272.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  20273.         @scrollopt output, @ccopt output, @rows output
  20274.  
  20275.     drop table #spprocparamrowset1
  20276.        return isnull(@ret,0)
  20277. go
  20278. dump tran master with no_log
  20279. go
  20280. create procedure sp_procedure_params_rowset;3
  20281. as
  20282.     select
  20283.         PROCEDURE_CATALOG     = convert(sysname, ' '),
  20284.         PROCEDURE_SCHEMA     = convert(sysname, ' '),
  20285.         PROCEDURE_NAME         = convert(varchar(35), ' '),
  20286.         PARAMETER_NAME         = convert(sysname, ' '),
  20287.         ORDINAL_POSITION     = convert(smallint, 0),
  20288.         PARAMETER_TYPE         = convert(smallint, 0),
  20289.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20290.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20291.         IS_NULLABLE        = convert(bit, 0),
  20292.         DATA_TYPE        = convert(smallint, 0),
  20293.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  20294.         CHARACTER_OCTET_LENGTH    = convert(int, 0),
  20295.         NUMERIC_PRECISION    = convert(smallint, 0),
  20296.         NUMERIC_SCALE        = convert(smallint, 0),
  20297.         DESCRIPTION        = convert(varchar(1),null),
  20298.         TYPE_NAME        = convert(sysname,null),
  20299.         LOCAL_TYPE_NAME        = convert(sysname,null)
  20300.     where    1=0
  20301. go
  20302. dump tran master with no_log
  20303. go
  20304.  
  20305.  
  20306. if (charindex('7.00', @@version) > 0 or
  20307.     charindex('8.00', @@version) > 0)
  20308.     drop procedure sp_procedure_params_rowset
  20309. else
  20310. begin
  20311.     print ''
  20312.     print ''
  20313.     print 'Warning:'
  20314.     print 'you are installing the stored procedures '
  20315.     print 'on a pre 7.0 SQL Server.'
  20316.     print 'Ignore the following errors.'
  20317. end
  20318. go
  20319.  
  20320. /*    Procedure for 7.0 servers */
  20321. create procedure sp_procedure_params_rowset
  20322.     (
  20323.        @procedure_name        sysname,
  20324.     @group_number        int = 1,
  20325.     @procedure_schema     sysname = null,
  20326.     @parameter_name        sysname = null
  20327.     )
  20328. as
  20329.     select
  20330.         PROCEDURE_CATALOG     = db_name(),
  20331.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20332.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20333.         PARAMETER_NAME         = c.name,
  20334.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20335.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20336.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20337.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20338.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20339.         DATA_TYPE        = d.oledb_data_type,
  20340.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20341.                         case 
  20342.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20343.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20344.                         then coalesce(d.column_size,c.length)
  20345.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20346.                         then coalesce(d.column_size,c.length/2)
  20347.                         else null 
  20348.                         end),
  20349.         CHARACTER_OCTET_LENGTH    = convert(int,
  20350.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20351.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20352.                         then coalesce(d.column_size,c.length)
  20353.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20354.                         then coalesce(d.column_size*2,c.length)
  20355.                         else null 
  20356.                         end),
  20357.         NUMERIC_PRECISION    = convert(smallint,
  20358.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20359.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20360.                             then d.data_precision else null end),
  20361.         NUMERIC_SCALE        = convert(smallint, 
  20362.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20363.         DESCRIPTION        = convert(nvarchar(1),null),
  20364.         TYPE_NAME        = d.type_name,
  20365.         LOCAL_TYPE_NAME        = d.local_type_name
  20366.     
  20367.     from
  20368.         sysobjects o,
  20369.         syscolumns c,
  20370.         master.dbo.spt_provider_types d,
  20371.         systypes t
  20372.     where
  20373.         o.name = @procedure_name
  20374.     and    o.type = 'P'                            /* Just Procedures */
  20375.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20376.     and    o.id = c.id
  20377.     and    c.number = @group_number
  20378.     and    c.xtype = d.ss_dtype
  20379.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20380.     and    c.xusertype = t.xusertype
  20381.     and    (@parameter_name is null or @parameter_name = c.name)
  20382.     UNION ALL
  20383.     SELECT           /* return value row*/
  20384.         PROCEDURE_CATALOG     = db_name(),
  20385.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20386.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20387.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20388.         ORDINAL_POSITION     = convert(smallint,0),
  20389.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20390.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20391.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20392.         IS_NULLABLE        = convert(bit,0),
  20393.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20394.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20395.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20396.         NUMERIC_PRECISION    = convert(smallint,10),
  20397.         NUMERIC_SCALE        = convert(smallint,null),
  20398.         DESCRIPTION        = convert(nvarchar(1),null),
  20399.         TYPE_NAME        = convert(sysname,N'int'),
  20400.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20401.     from
  20402.         sysobjects o,
  20403.         syscomments c
  20404.     where
  20405.         o.name = @procedure_name
  20406.     and    o.id = c.id 
  20407.     and    c.number = @group_number
  20408.     and    c.colid = 1
  20409.     and    o.type = 'P'                        /* Just Procedures */
  20410.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20411.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20412.     order by 2, 3, 5
  20413. go
  20414. dump tran master with no_log
  20415. go
  20416. create procedure sp_procedure_params_rowset;2
  20417.     (
  20418.     @procedure_schema     sysname = null,
  20419.     @parameter_name        sysname = null
  20420.     )
  20421. as
  20422.     select
  20423.         PROCEDURE_CATALOG     = db_name(),
  20424.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20425.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20426.         PARAMETER_NAME         = c.name,
  20427.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20428.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20429.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20430.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20431.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20432.         DATA_TYPE        = d.oledb_data_type,
  20433.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20434.                         case 
  20435.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20436.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20437.                         then coalesce(d.column_size,c.length)
  20438.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20439.                         then coalesce(d.column_size,c.length/2)
  20440.                         else null 
  20441.                         end),
  20442.         CHARACTER_OCTET_LENGTH    = convert(int,
  20443.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20444.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20445.                         then coalesce(d.column_size,c.length)
  20446.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20447.                         then coalesce(d.column_size*2,c.length)
  20448.                         else null 
  20449.                         end),
  20450.         NUMERIC_PRECISION    = convert(smallint,
  20451.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20452.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20453.                             then d.data_precision else null end),
  20454.         NUMERIC_SCALE        = convert(smallint, 
  20455.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20456.         DESCRIPTION        = convert(nvarchar(1),null),
  20457.         TYPE_NAME        = d.type_name,
  20458.         LOCAL_TYPE_NAME        = d.local_type_name
  20459.     
  20460.     from
  20461.         sysobjects o,
  20462.         syscolumns c,
  20463.         master.dbo.spt_provider_types d,
  20464.         systypes t
  20465.     where
  20466.         o.type = 'P'                            /* Just Procedures */
  20467.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20468.     and    o.id = c.id
  20469.     and    c.xtype = d.ss_dtype
  20470.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20471.     and    c.xusertype = t.xusertype
  20472.     and    (@parameter_name is null or @parameter_name = c.name)
  20473.     UNION ALL
  20474.     SELECT           /* return value row*/
  20475.         PROCEDURE_CATALOG     = db_name(),
  20476.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20477.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20478.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20479.         ORDINAL_POSITION     = convert(smallint,0),
  20480.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20481.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20482.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20483.         IS_NULLABLE        = convert(bit,0),
  20484.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20485.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20486.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20487.         NUMERIC_PRECISION    = convert(smallint,10),
  20488.         NUMERIC_SCALE        = convert(smallint,null),
  20489.         DESCRIPTION        = convert(nvarchar(1),null),
  20490.         TYPE_NAME        = convert(sysname,N'int'),
  20491.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20492.     from
  20493.         sysobjects o,
  20494.         syscomments c
  20495.     where
  20496.         o.type = 'P'        /* Just Procedures */
  20497.     and    o.id = c.id 
  20498.     and    c.colid = 1
  20499.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20500.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20501.     order by 2, 3, 5
  20502. go
  20503.  
  20504. if (charindex('8.00', @@version) > 0)
  20505.     drop procedure sp_procedure_params_rowset
  20506. else
  20507. begin
  20508.     print ''
  20509.     print ''
  20510.     print 'Warning:'
  20511.     print 'you are installing the stored procedures '
  20512.     print 'on a pre 8.0 SQL Server.'
  20513.     print 'Ignore the following errors.'
  20514. end
  20515. go
  20516.  
  20517. /*    Procedure for 8.0 servers */
  20518. create procedure sp_procedure_params_rowset
  20519.     (
  20520.        @procedure_name        sysname,
  20521.     @group_number        int = 1,
  20522.     @procedure_schema     sysname = null,
  20523.     @parameter_name        sysname = null
  20524.     )
  20525. as
  20526.     select
  20527.         PROCEDURE_CATALOG     = db_name(),
  20528.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20529.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20530.         PARAMETER_NAME         = c.name,
  20531.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20532.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20533.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20534.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20535.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20536.         DATA_TYPE        = d.oledb_data_type,
  20537.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20538.                         case 
  20539.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20540.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20541.                         then coalesce(d.column_size,c.length)
  20542.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20543.                         then coalesce(d.column_size,c.length/2)
  20544.                         else null 
  20545.                         end),
  20546.         CHARACTER_OCTET_LENGTH    = convert(int,
  20547.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20548.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20549.                         then coalesce(d.column_size,c.length)
  20550.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20551.                         then coalesce(d.column_size*2,c.length)
  20552.                         else null 
  20553.                         end),
  20554.         NUMERIC_PRECISION    = convert(smallint,
  20555.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20556.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20557.                             then d.data_precision else null end),
  20558.         NUMERIC_SCALE        = convert(smallint, 
  20559.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20560.         DESCRIPTION        = convert(nvarchar(1),null),
  20561.         TYPE_NAME        = d.type_name,
  20562.         LOCAL_TYPE_NAME        = d.local_type_name
  20563.     
  20564.     from
  20565.         sysobjects o,
  20566.         syscolumns c,
  20567.         master.dbo.spt_provider_types d,
  20568.         systypes t
  20569.     where
  20570.         o.name = @procedure_name
  20571.     and     (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  20572.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20573.     and    o.id = c.id
  20574.     and    ((c.number = @group_number and o.type = 'P') 
  20575.             or (c.number = 0 and o.type = 'FN')
  20576.             or (c.number = 1 and o.type in ('TF', 'IF')))
  20577.     and    c.xtype = d.ss_dtype
  20578.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20579.     and    c.xusertype = t.xusertype
  20580.     and    (@parameter_name is null or @parameter_name = c.name)
  20581.     UNION ALL
  20582.     SELECT           /* return value row*/
  20583.         PROCEDURE_CATALOG     = db_name(),
  20584.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20585.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20586.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20587.         ORDINAL_POSITION     = convert(smallint,0),
  20588.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20589.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20590.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20591.         IS_NULLABLE        = convert(bit,0),
  20592.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20593.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20594.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20595.         NUMERIC_PRECISION    = convert(smallint,10),
  20596.         NUMERIC_SCALE        = convert(smallint,null),
  20597.         DESCRIPTION        = convert(nvarchar(1),null),
  20598.         TYPE_NAME        = convert(sysname,N'int'),
  20599.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20600.     from
  20601.         sysobjects o,
  20602.         syscomments c
  20603.     where
  20604.         o.name = @procedure_name
  20605.     and    o.id = c.id 
  20606.     and    c.number = @group_number
  20607.     and    c.colid = 1
  20608.     and    o.type = 'P'            /* Just Procedures */
  20609.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20610.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20611.     UNION ALL
  20612.     SELECT           /* UDF return value row*/
  20613.         PROCEDURE_CATALOG          = db_name(),
  20614.         PROCEDURE_SCHEMA          = user_name(o.uid),
  20615.         PROCEDURE_NAME              = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20616.         PARAMETER_NAME              = convert(sysname,'@RETURN_VALUE'),
  20617.         ORDINAL_POSITION          = convert(smallint, 0),
  20618.         PARAMETER_TYPE              = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20619.         PARAMETER_HASDEFAULT     = convert(tinyint, 0),
  20620.         PARAMETER_DEFAULT         = convert(nvarchar(255),null),
  20621.         IS_NULLABLE                 = convert(bit, c.isnullable),
  20622.         DATA_TYPE                 = d.oledb_data_type,
  20623.         CHARACTER_MAXIMUM_LENGTH = convert(int,
  20624.                         case 
  20625.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20626.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20627.                         then coalesce(d.column_size,c.length)
  20628.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20629.                         then coalesce(d.column_size,c.length/2)
  20630.                         else null 
  20631.                         end),
  20632.         CHARACTER_OCTET_LENGTH     = convert(int,
  20633.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20634.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20635.                         then coalesce(d.column_size,c.length)
  20636.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20637.                         then coalesce(d.column_size*2,c.length)
  20638.                         else null 
  20639.                         end),
  20640.         NUMERIC_PRECISION         = convert(smallint,
  20641.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20642.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20643.                             then d.data_precision else null end),
  20644.         NUMERIC_SCALE             = convert(smallint, 
  20645.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20646.         DESCRIPTION                 = convert(nvarchar(1),null),
  20647.         TYPE_NAME                 = d.type_name,
  20648.         LOCAL_TYPE_NAME             = d.local_type_name
  20649.     FROM
  20650.         sysobjects o,
  20651.         syscolumns c,
  20652.         master.dbo.spt_provider_types d,
  20653.         systypes t
  20654.     WHERE
  20655.         o.name = @procedure_name
  20656.     and    o.id = c.id 
  20657.     and    c.number = 0
  20658.     and    c.colid = 0
  20659.     and    o.type = 'FN'            /* UDF scalar functions */
  20660.     and    c.xtype = d.ss_dtype
  20661.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20662.     and    c.xusertype = t.xusertype
  20663.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20664.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20665.     UNION ALL
  20666.     SELECT           /* UDF table value row*/
  20667.         PROCEDURE_CATALOG     = db_name(),
  20668.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20669.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20670.         PARAMETER_NAME         = convert(sysname,'@TABLE_RETURN_VALUE'),
  20671.         ORDINAL_POSITION     = convert(smallint, 0),
  20672.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20673.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20674.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20675.         IS_NULLABLE        = convert(bit,0),
  20676.         DATA_TYPE        = convert(smallint, 0),                /*DBTYPE_EMPTY*/
  20677.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20678.                         case 
  20679.                         when d.oledb_data_type = 129         /*DBTYPE_STR*/ 
  20680.                             or d.oledb_data_type = 128    /*DBTYPE_BYTES*/
  20681.                         then coalesce(d.column_size,c.length)
  20682.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  20683.                         then coalesce(d.column_size,c.length/2)
  20684.                         else null 
  20685.                         end),
  20686.         CHARACTER_OCTET_LENGTH    = convert(int,
  20687.                         case when d.oledb_data_type = 129    /*DBTYPE_STR*/ 
  20688.                             or d.oledb_data_type = 128    /*DBTYPE_BYTES*/
  20689.                         then coalesce(d.column_size,c.length)
  20690.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  20691.                         then coalesce(d.column_size*2,c.length)
  20692.                         else null 
  20693.                         end),
  20694.         NUMERIC_PRECISION    = convert(smallint,
  20695.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20696.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20697.                             then d.data_precision else null end),
  20698.         NUMERIC_SCALE        = convert(smallint, 
  20699.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20700.         DESCRIPTION        = convert(nvarchar(50), N'Result table returned by table valued function'),
  20701.         TYPE_NAME        = N'table',
  20702.         LOCAL_TYPE_NAME        = N'table'
  20703.     
  20704.     from
  20705.         sysobjects o,
  20706.         syscolumns c,
  20707.         master.dbo.spt_provider_types d
  20708.     where
  20709.         o.name = @procedure_name
  20710.     and    o.id = c.id 
  20711.     and    c.number = 0
  20712.     and    c.colid = 1
  20713.     and    o.type in ('TF', 'IF')            /* UDF table functions */
  20714.     and    c.xtype = d.ss_dtype
  20715.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20716.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20717.     and    (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  20718.     order by 2, 3, 5
  20719. go
  20720. dump tran master with no_log
  20721. go
  20722. create procedure sp_procedure_params_rowset;2
  20723.     (
  20724.     @procedure_schema     sysname = null,
  20725.     @parameter_name        sysname = null
  20726.     )
  20727. as
  20728.     select
  20729.         PROCEDURE_CATALOG     = db_name(),
  20730.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20731.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20732.         PARAMETER_NAME         = c.name,
  20733.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20734.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20735.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20736.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20737.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20738.         DATA_TYPE        = d.oledb_data_type,
  20739.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20740.                         case 
  20741.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20742.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20743.                         then coalesce(d.column_size,c.length)
  20744.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20745.                         then coalesce(d.column_size,c.length/2)
  20746.                         else null 
  20747.                         end),
  20748.         CHARACTER_OCTET_LENGTH    = convert(int,
  20749.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20750.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20751.                         then coalesce(d.column_size,c.length)
  20752.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20753.                         then coalesce(d.column_size*2,c.length)
  20754.                         else null 
  20755.                         end),
  20756.         NUMERIC_PRECISION    = convert(smallint,
  20757.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20758.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20759.                             then d.data_precision else null end),
  20760.         NUMERIC_SCALE        = convert(smallint, 
  20761.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20762.         DESCRIPTION        = convert(nvarchar(1),null),
  20763.         TYPE_NAME        = d.type_name,
  20764.         LOCAL_TYPE_NAME        = d.local_type_name
  20765.     
  20766.     from
  20767.         sysobjects o,
  20768.         syscolumns c,
  20769.         master.dbo.spt_provider_types d,
  20770.         systypes t
  20771.     where
  20772.         (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  20773.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20774.     and    o.id = c.id
  20775.     and    (o.type = 'P' or (c.number = 0 and o.type = 'FN') or (c.number = 1 and o.type in ('TF', 'IF')))
  20776.     and    c.xtype = d.ss_dtype
  20777.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20778.     and    c.xusertype = t.xusertype
  20779.     and    (@parameter_name is null or @parameter_name = c.name)
  20780.     UNION ALL
  20781.     SELECT           /* return value row*/
  20782.         PROCEDURE_CATALOG     = db_name(),
  20783.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20784.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20785.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20786.         ORDINAL_POSITION     = convert(smallint,0),
  20787.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20788.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20789.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20790.         IS_NULLABLE        = convert(bit,0),
  20791.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20792.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20793.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20794.         NUMERIC_PRECISION    = convert(smallint,10),
  20795.         NUMERIC_SCALE        = convert(smallint,null),
  20796.         DESCRIPTION        = convert(nvarchar(1),null),
  20797.         TYPE_NAME        = convert(sysname,N'int'),
  20798.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20799.     from
  20800.         sysobjects o,
  20801.         syscomments c
  20802.     where
  20803.         o.type = 'P'            /* Just Procedures */
  20804.     and    o.id = c.id 
  20805.     and    c.colid = 1
  20806.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20807.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20808.     UNION ALL
  20809.     SELECT           /* UDF return value row*/
  20810.         PROCEDURE_CATALOG     = db_name(),
  20811.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20812.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20813.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20814.         ORDINAL_POSITION     = convert(smallint, 0),
  20815.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20816.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20817.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20818.         IS_NULLABLE        = convert(bit, c.isnullable),
  20819.         DATA_TYPE        = d.oledb_data_type,
  20820.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20821.                         case 
  20822.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20823.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20824.                         then coalesce(d.column_size,c.length)
  20825.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20826.                         then coalesce(d.column_size,c.length/2)
  20827.                         else null 
  20828.                         end),
  20829.         CHARACTER_OCTET_LENGTH    = convert(int,
  20830.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20831.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20832.                         then coalesce(d.column_size,c.length)
  20833.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20834.                         then coalesce(d.column_size*2,c.length)
  20835.                         else null 
  20836.                         end),
  20837.         NUMERIC_PRECISION    = convert(smallint,
  20838.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20839.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20840.                             then d.data_precision else null end),
  20841.         NUMERIC_SCALE        = convert(smallint, 
  20842.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20843.         DESCRIPTION        = convert(nvarchar(1),null),
  20844.         TYPE_NAME        = d.type_name,
  20845.         LOCAL_TYPE_NAME        = d.local_type_name
  20846.     
  20847.     from
  20848.         sysobjects o,
  20849.         syscolumns c,
  20850.         master.dbo.spt_provider_types d,
  20851.         systypes t
  20852.     where
  20853.         o.id = c.id 
  20854.     and    c.number = 0
  20855.     and    c.colid = 0
  20856.     and    o.type = 'FN'            /* UDF scalar functions */
  20857.     and    c.xtype = d.ss_dtype
  20858.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20859.     and    c.xusertype = t.xusertype
  20860.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20861.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20862.     UNION ALL
  20863.     SELECT           /* UDF table value row*/
  20864.         PROCEDURE_CATALOG     = db_name(),
  20865.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20866.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20867.         PARAMETER_NAME         = convert(sysname,'@TABLE_RETURN_VALUE'),
  20868.         ORDINAL_POSITION     = convert(smallint, 0),
  20869.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20870.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20871.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20872.         IS_NULLABLE        = convert(bit,0),
  20873.         DATA_TYPE        = convert(smallint, 0),            /*DBTYPE_EMPTY*/
  20874.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20875.                         case 
  20876.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20877.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20878.                         then coalesce(d.column_size,c.length)
  20879.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20880.                         then coalesce(d.column_size,c.length/2)
  20881.                         else null 
  20882.                         end),
  20883.         CHARACTER_OCTET_LENGTH    = convert(int,
  20884.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20885.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20886.                         then coalesce(d.column_size,c.length)
  20887.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20888.                         then coalesce(d.column_size*2,c.length)
  20889.                         else null 
  20890.                         end),
  20891.         NUMERIC_PRECISION    = convert(smallint,
  20892.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20893.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20894.                             then d.data_precision else null end),
  20895.         NUMERIC_SCALE        = convert(smallint, 
  20896.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20897.         DESCRIPTION        = convert(nvarchar(1),null),
  20898.         TYPE_NAME        = N'table',
  20899.         LOCAL_TYPE_NAME        = N'table'
  20900.     
  20901.     from
  20902.         sysobjects o,
  20903.         syscolumns c,
  20904.         master.dbo.spt_provider_types d
  20905.     where
  20906.         o.id = c.id 
  20907.     and    c.number = 0
  20908.     and    c.colid = 1
  20909.     and    o.type in ('TF', 'IF')            /* UDF table functions */
  20910.     and    c.xtype = d.ss_dtype
  20911.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20912.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20913.     and    (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  20914.     order by 2, 3, 5
  20915. go
  20916.  
  20917. grant execute on sp_procedure_params_rowset to public
  20918. go
  20919.  
  20920. dump tran master with no_log
  20921. go
  20922. if (charindex('6.00', @@version) > 0)
  20923.     begin
  20924.     if (exists (select * from sysobjects
  20925.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  20926.         begin
  20927.         drop procedure sp_procedure_params_rowset
  20928.         dump tran master with no_log
  20929.         end
  20930.     end
  20931. go
  20932.  
  20933.  
  20934. print ''
  20935. print 'creating sp_procedures_rowset'
  20936. go
  20937.  
  20938.  
  20939. /* pre 7.0 version */
  20940. create procedure sp_procedures_rowset
  20941.     (
  20942.     @procedure_name        varchar(255), 
  20943.     @group_number        int = 1,
  20944.     @procedure_schema    varchar(255) = null
  20945.     )        
  20946. as
  20947.     select
  20948.         PROCEDURE_CATALOG    = db_name(),
  20949.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20950.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  20951.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  20952.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  20953.         DESCRIPTION        = convert(varchar(1),null),
  20954.         DATE_CREATED        = o.crdate,
  20955.         DATE_MODIFIED        = convert(datetime,null)
  20956.     from     
  20957.         sysobjects o, 
  20958.         syscomments p,
  20959.         sysusers u
  20960.     where
  20961.             o.name = @procedure_name
  20962.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20963.     and     o.type = 'P'        /* Object type of Procedure */
  20964.     and     p.colid = 1
  20965.     and     p.id = o.id
  20966.     and    p.number = @group_number
  20967.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  20968.     and     (    suser_id() = 1     /* User is the System Administrator */
  20969.         or     o.uid = user_id()    /* User created the object */
  20970.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  20971.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  20972.              from sysprotects p
  20973.              where p.id = o.id
  20974.                  /*  get rows for public,current user,user's group */
  20975.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  20976.                  /* check for SELECT,EXECUTE privilege */
  20977.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  20978.             ) = 1     /* final magic...compare Grants    */
  20979.         )
  20980.     order by 2, 3
  20981. go
  20982. dump tran master with no_log
  20983. go
  20984. create procedure sp_procedures_rowset;2
  20985.     (
  20986.     @procedure_schema    varchar(255) = null
  20987.     )        
  20988. as
  20989.     select
  20990.         PROCEDURE_CATALOG    = db_name(),
  20991.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20992.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  20993.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  20994.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  20995.         DESCRIPTION        = convert(varchar(1),null),
  20996.         DATE_CREATED        = o.crdate,
  20997.         DATE_MODIFIED        = convert(datetime,null)
  20998.     from     
  20999.         sysobjects o, 
  21000.         syscomments p,
  21001.         sysusers u
  21002.     where
  21003.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21004.     and     o.type = 'P'        /* Object type of Procedure */
  21005.     and     p.colid = 1
  21006.     and     p.id = o.id
  21007.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  21008.     and     (    suser_id() = 1     /* User is the System Administrator */
  21009.         or     o.uid = user_id()    /* User created the object */
  21010.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21011.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21012.              from sysprotects p
  21013.              where p.id = o.id
  21014.                  /*  get rows for public,current user,user's group */
  21015.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21016.                  /* check for SELECT,EXECUTE privilege */
  21017.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  21018.             ) = 1     /* final magic...compare Grants    */
  21019.         )
  21020.     order by 2, 3
  21021. go
  21022. dump tran master with no_log
  21023. go
  21024.  
  21025. if (charindex('7.00', @@version) > 0 or
  21026.     charindex('8.00', @@version) > 0)
  21027.     drop procedure sp_procedures_rowset
  21028. else
  21029. begin
  21030.     print ''
  21031.     print ''
  21032.     print 'Warning:'
  21033.     print 'you are installing the stored procedures '
  21034.     print 'on a pre 7.0 SQL Server.'
  21035.     print 'Ignore the following errors.'
  21036. end
  21037. go
  21038.  
  21039. /* 7.0 version */
  21040. create procedure sp_procedures_rowset
  21041.     (
  21042.     @procedure_name        sysname, 
  21043.     @group_number        int = 1,
  21044.     @procedure_schema    sysname = null
  21045.     )        
  21046. as
  21047.     select
  21048.         PROCEDURE_CATALOG    = db_name(),
  21049.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21050.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21051.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21052.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21053.         DESCRIPTION        = convert(nvarchar(1),null),
  21054.         DATE_CREATED        = o.crdate,
  21055.         DATE_MODIFIED        = convert(datetime,null)
  21056.     from     
  21057.         sysobjects o, 
  21058.         syscomments p
  21059.     where
  21060.         permissions(o.id) <> 0
  21061.     and    o.name = @procedure_name
  21062.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21063.     and     o.type = 'P'        /* Object type of Procedure */
  21064.     and     p.colid = 1
  21065.     and     p.id = o.id
  21066.     and    p.number = @group_number
  21067.     order by 2, 3
  21068. go
  21069. dump tran master with no_log
  21070. go
  21071. create procedure sp_procedures_rowset;2
  21072.     (
  21073.     @procedure_schema    sysname = null
  21074.     )        
  21075. as
  21076.     select
  21077.         PROCEDURE_CATALOG    = db_name(),
  21078.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21079.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21080.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21081.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21082.         DESCRIPTION        = convert(nvarchar(1),null),
  21083.         DATE_CREATED        = o.crdate,
  21084.         DATE_MODIFIED        = convert(datetime,null)
  21085.     from     sysobjects o, syscomments p
  21086.     where
  21087.         permissions(o.id) <> 0
  21088.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21089.     and     o.type = 'P'        /* Object type of Procedure */
  21090.     and p.colid = 1
  21091.     and p.id = o.id
  21092.     order by 2, 3
  21093. go
  21094.  
  21095. if (charindex('8.00', @@version) > 0)
  21096.     drop procedure sp_procedures_rowset
  21097. else
  21098. begin
  21099.     print ''
  21100.     print ''
  21101.     print 'Warning:'
  21102.     print 'you are installing the stored procedures '
  21103.     print 'on a pre 8.0 SQL Server.'
  21104.     print 'Ignore the following errors.'
  21105. end
  21106. go
  21107.  
  21108. /* 8.0 version */
  21109. create procedure sp_procedures_rowset
  21110.     (
  21111.     @procedure_name        sysname, 
  21112.     @group_number        int = 1,
  21113.     @procedure_schema    sysname = null
  21114.     )        
  21115. as
  21116.     select
  21117.         PROCEDURE_CATALOG    = db_name(),
  21118.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21119.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21120.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21121.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21122.         DESCRIPTION        = convert(nvarchar(1),null),
  21123.         DATE_CREATED        = o.crdate,
  21124.         DATE_MODIFIED        = convert(datetime,null)
  21125.     from     
  21126.         sysobjects o, 
  21127.         syscomments p
  21128.     where
  21129.         permissions(o.id) <> 0
  21130.     and    o.name = @procedure_name
  21131.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21132.     and     o.type in ('P', 'FN', 'TF', 'IF')    /* Object type of Procedure */
  21133.     and     p.colid = 1
  21134.     and     p.id = o.id
  21135.     and    p.number = @group_number
  21136.     order by 2, 3
  21137. go
  21138. dump tran master with no_log
  21139. go
  21140. create procedure sp_procedures_rowset;2
  21141.     (
  21142.     @procedure_schema    sysname = null
  21143.     )        
  21144. as
  21145.     select
  21146.         PROCEDURE_CATALOG    = db_name(),
  21147.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21148.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21149.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21150.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21151.         DESCRIPTION        = convert(nvarchar(1),null),
  21152.         DATE_CREATED        = o.crdate,
  21153.         DATE_MODIFIED        = convert(datetime,null)
  21154.     from     sysobjects o, syscomments p
  21155.     where
  21156.         permissions(o.id) <> 0
  21157.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21158.     and     o.type in ('P', 'FN', 'TF', 'IF')    /* Object type of Procedure */
  21159.     and p.colid = 1
  21160.     and p.id = o.id
  21161.     order by 2, 3
  21162. go
  21163.  
  21164. grant execute on sp_procedures_rowset to public
  21165. go
  21166.  
  21167. dump tran master with no_log
  21168. go
  21169.  
  21170. if (charindex('6.00', @@version) > 0)
  21171.     begin
  21172.     if (exists (select * from sysobjects
  21173.             where name = 'sp_procedures_rowset' and type = 'P '))
  21174.         begin
  21175.         drop procedure sp_procedures_rowset
  21176.         dump tran master with no_log
  21177.         end
  21178.     end
  21179. go
  21180.  
  21181.  
  21182. print ''
  21183. print 'creating sp_schemata_rowset'
  21184. go
  21185.  
  21186. /*    6.0 and 6.5 and 7.0 version */
  21187. create procedure sp_schemata_rowset
  21188.     (
  21189.     @schema_name    varchar(90) = null, 
  21190.     @schema_owner    varchar(90) = null
  21191.     )        
  21192. as
  21193.     select    distinct
  21194.         CATALOG_NAME            = db_name(),
  21195.         SCHEMA_NAME            = user_name(o.uid),    
  21196.         SCHEMA_OWNER            = user_name(o.uid),    
  21197.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,'master'),
  21198.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  21199.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  21200.     from    
  21201.         sysobjects o,
  21202.         master.dbo.sysconfigures    cfg,
  21203.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  21204.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  21205.     where   
  21206.         (@schema_name is null or @schema_name = user_name(o.uid))
  21207.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21208.     and    a_cha.type = 1001 /* type is charset */
  21209.     and     b_cha.type = 2001 /* type is sortorder */
  21210.     and     a_cha.id = b_cha.csid
  21211.     and     b_cha.id = cfg.value
  21212.     order by 2
  21213. go
  21214. dump tran master with no_log
  21215. go
  21216.  
  21217. if (charindex('8.00', @@version) > 0)
  21218.     drop procedure sp_schemata_rowset
  21219. else
  21220. begin
  21221.     print ''
  21222.     print ''
  21223.     print 'Warning:'
  21224.     print 'you are installing the stored procedures '
  21225.     print 'on a pre 8.0 SQL Server.'
  21226.     print 'Ignore the following errors.'
  21227. end
  21228. go
  21229.  
  21230. /* 8.0 version */
  21231. create procedure sp_schemata_rowset
  21232.     (
  21233.     @schema_name    sysname = null, 
  21234.     @schema_owner    sysname = null
  21235.     )        
  21236. as
  21237.     select    distinct
  21238.         CATALOG_NAME            = db_name(),
  21239.         SCHEMA_NAME            = user_name(o.uid),    
  21240.         SCHEMA_OWNER            = user_name(o.uid),    
  21241.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,N'master'),
  21242.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  21243.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  21244.     from    
  21245.         sysobjects o,
  21246.         master.dbo.syscharsets        a_cha /* charset/1001, not sortorder */
  21247.     where   
  21248.         (@schema_name is null or @schema_name = user_name(o.uid))
  21249.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21250.     and    a_cha.type = 1001 /* type is charset */
  21251.     and     a_cha.id = convert(tinyint, DatabasePropertyEx(db_name(), 'sqlcharset')) /* what is charset of a table? */
  21252.     order by 2
  21253. go
  21254.  
  21255. dump tran master with no_log
  21256. go
  21257.  
  21258. /* The following stored procedure is used for Sphinx and Hydra */
  21259. create procedure sp_schemata_rowset;3
  21260. as
  21261.     select
  21262.         CATALOG_NAME            = convert(sysname,' '),
  21263.         SCHEMA_NAME            = convert(sysname,' '),    
  21264.         SCHEMA_OWNER            = convert(sysname,' '),    
  21265.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,' '),
  21266.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  21267.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,' ')
  21268.     where    1=0
  21269. go
  21270. grant execute on sp_schemata_rowset to public
  21271. go
  21272.  
  21273. dump tran master with no_log
  21274. go
  21275. if (charindex('6.00', @@version) > 0)
  21276.     begin
  21277.     if (exists (select * from sysobjects
  21278.             where name = 'sp_schemata_rowset' and type = 'P '))
  21279.         begin
  21280.         drop procedure sp_schemata_rowset
  21281.         dump tran master with no_log
  21282.         end
  21283.     end
  21284. go
  21285.  
  21286.  
  21287. print ''
  21288. print 'creating sp_statistics_rowset'
  21289. go
  21290.  
  21291.  
  21292. /*    6.0 and 6.5 version */
  21293. create procedure sp_statistics_rowset
  21294.     (
  21295.     @table_name    varchar(255),
  21296.     @table_schema    varchar(255) = null     
  21297.     )
  21298. as
  21299.     select    db_name()                as TABLE_CATALOG,        
  21300.         user_name(o.uid)            as TABLE_SCHEMA,
  21301.         o.name                    as TABLE_NAME,
  21302.         x.rows                    as CARDINALITY
  21303.     from    sysobjects o, sysindexes x, sysusers u
  21304.     where    o.type in ('U')
  21305.     and     o.name = @table_name
  21306.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21307.     and     x.id = o.id
  21308.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21309.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21310.     and     (
  21311.         suser_id() = 1     /* User is the System Administrator */
  21312.         or o.uid = user_id()     /* User created the object */
  21313.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21314.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21315.             from sysprotects p
  21316.             where p.id = o.id
  21317.             /* get rows for public,current user,user's group */
  21318.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21319.             /* check for SELECT,EXECUTE privilege */
  21320.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21321.             ) = 1    /* final magic...compare Grants      */
  21322.            )
  21323.     order by 2, 3
  21324. go
  21325. dump tran master with no_log
  21326. go
  21327. create procedure sp_statistics_rowset;2
  21328.     (
  21329.     @table_schema    varchar(255) = null     
  21330.     )
  21331. as
  21332.     select    db_name()                as TABLE_CATALOG,        
  21333.         user_name(o.uid)            as TABLE_SCHEMA,
  21334.         o.name                    as TABLE_NAME,
  21335.         x.rows                    as CARDINALITY
  21336.     from    sysobjects o, sysindexes x, sysusers u
  21337.     where    o.type in ('U')
  21338.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21339.     and     x.id = o.id
  21340.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21341.     and     (
  21342.         suser_id() = 1     /* User is the System Administrator */
  21343.         or o.uid = user_id()     /* User created the object */
  21344.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21345.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21346.             from sysprotects p
  21347.             where p.id = o.id
  21348.             /* get rows for public,current user,user's group */
  21349.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21350.             /* check for SELECT,EXECUTE privilege */
  21351.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21352.             ) = 1    /* final magic...compare Grants      */
  21353.            )        
  21354.     order by 2, 3
  21355. go
  21356. dump tran master with no_log
  21357. go
  21358.  
  21359. if (charindex('7.00', @@version) > 0 or
  21360.     charindex('8.00', @@version) > 0)
  21361.     drop procedure sp_statistics_rowset
  21362. else
  21363. begin
  21364.     print ''
  21365.     print ''
  21366.     print 'Warning:'
  21367.     print 'you are installing the stored procedures '
  21368.     print 'on a pre 8.0 SQL Server.'
  21369.     print 'Ignore the following errors.'
  21370. end
  21371. go
  21372.  
  21373. /*    8.0 version */
  21374. create procedure sp_statistics_rowset
  21375.     (
  21376.     @table_name    sysname,
  21377.     @table_schema    sysname = null     
  21378.     )
  21379. as
  21380.     select    db_name()                as TABLE_CATALOG,        
  21381.         user_name(o.uid)            as TABLE_SCHEMA,
  21382.         o.name                    as TABLE_NAME,
  21383.         x.rows                    as CARDINALITY
  21384.     from    sysobjects o, sysindexes x
  21385.     where    o.type in ('U')
  21386.     and     o.name = @table_name
  21387.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21388.     and     x.id = o.id
  21389.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21390.     and     permissions(o.id) <> 0
  21391.     order by 2, 3
  21392. go
  21393. dump tran master with no_log
  21394. go
  21395. create procedure sp_statistics_rowset;2
  21396.     (
  21397.     @table_schema    sysname = null     
  21398.     )
  21399. as
  21400.     select    db_name()                as TABLE_CATALOG,        
  21401.         user_name(o.uid)            as TABLE_SCHEMA,
  21402.         o.name                    as TABLE_NAME,
  21403.         x.rows                    as CARDINALITY
  21404.     from    sysobjects o, sysindexes x
  21405.     where    o.type in ('U')
  21406.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21407.     and     x.id = o.id
  21408.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21409.     and     permissions(o.id) <> 0
  21410.     order by 2, 3
  21411. go
  21412.  
  21413. grant execute on sp_statistics_rowset to public
  21414. go
  21415.  
  21416. dump tran master with no_log
  21417. go
  21418. if (charindex('6.00', @@version) > 0)
  21419.     begin
  21420.     if (exists (select * from sysobjects
  21421.             where name = 'sp_statistics_rowset' and type = 'P '))
  21422.         begin
  21423.         drop procedure sp_statistics_rowset
  21424.         dump tran master with no_log
  21425.         end
  21426.     end
  21427. go
  21428.  
  21429.  
  21430. print ''
  21431. print 'creating sp_tables_rowset'
  21432. go
  21433.  
  21434.  
  21435. /*    Procedure for 6.50 and earlier servers */
  21436. create procedure sp_tables_rowset
  21437.     (
  21438.     @table_name    varchar(255), 
  21439.     @table_schema    varchar(255) = null,    
  21440.     @table_type    varchar(255) = null 
  21441.     )
  21442. as
  21443.     select    TABLE_CATALOG    = db_name(),
  21444.         TABLE_SCHEMA    = user_name(o.uid),
  21445.         TABLE_NAME    = o.name,
  21446.         TABLE_TYPE    = convert(varchar(30),
  21447.                     case o.type 
  21448.                     when 'U' then 'TABLE'
  21449.                     when 'V' then 'VIEW'
  21450.                     when 'S' then 'SYSTEM TABLE'
  21451.                     end),
  21452.         TABLE_GUID    = convert(binary(16), null),
  21453.         DESCRIPTION    = convert(varchar(1), null),
  21454.         TABLE_PROPID    = convert(int,null),
  21455.         DATE_CREATED    = o.crdate,
  21456.         DATE_MODIFIED    = convert(datetime,null)
  21457.     from    sysusers u, sysobjects o
  21458.     where    o.type in ('U','V','S')
  21459.     and     o.name = @table_name
  21460.     and     (    @table_schema is null
  21461.         or    @table_schema = user_name(o.uid)
  21462.         )
  21463.     and     (
  21464.             @table_type is null
  21465.         or    @table_type = case o.type 
  21466.                     when 'U' then 'TABLE'
  21467.                     when 'V' then 'VIEW'
  21468.                     when 'S' then 'SYSTEM TABLE'
  21469.                     end
  21470.         )
  21471.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21472.     and     (
  21473.         suser_id() = 1     /* User is the System Administrator */
  21474.         or o.uid = user_id()     /* User created the object */
  21475.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21476.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21477.             from sysprotects p
  21478.             /* join to correlate with all rows in sysobjects */
  21479.             where p.id = o.id
  21480.             /* get rows for public,current user,user's group */
  21481.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21482.             /* check for SELECT,EXECUTE privilege */
  21483.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21484.             ) = 1    /* final magic...compare Grants      */
  21485.         )        
  21486.     order by 4, 2, 3
  21487. go
  21488. dump tran master with no_log
  21489. go
  21490. create procedure sp_tables_rowset;2
  21491.     (
  21492.     @table_schema    varchar(255) = null,    
  21493.     @table_type    varchar(255) = null 
  21494.     )
  21495. as
  21496.     select    TABLE_CATALOG    = db_name(),
  21497.         TABLE_SCHEMA    = user_name(o.uid),
  21498.         TABLE_NAME    = o.name,
  21499.         TABLE_TYPE    = convert(varchar(30),
  21500.                     case o.type 
  21501.                     when 'U' then 'TABLE'
  21502.                     when 'V' then 'VIEW'
  21503.                     when 'S' then 'SYSTEM TABLE'
  21504.                     end),
  21505.         TABLE_GUID    = convert(binary(16), null),
  21506.         DESCRIPTION    = convert(varchar(1), null),
  21507.         TABLE_PROPID    = convert(int,null),
  21508.         DATE_CREATED    = o.crdate,
  21509.         DATE_MODIFIED    = convert(datetime,null)
  21510.     from    sysusers u, sysobjects o
  21511.     where    o.type in ('U','V','S')
  21512.     and     (    @table_schema is null
  21513.         or    @table_schema = user_name(o.uid)
  21514.         )
  21515.     and     (
  21516.             @table_type is null
  21517.         or    @table_type = case o.type 
  21518.                     when 'U' then 'TABLE'
  21519.                     when 'V' then 'VIEW'
  21520.                     when 'S' then 'SYSTEM TABLE'
  21521.                     end
  21522.         )
  21523.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21524.     and     (
  21525.         suser_id() = 1     /* User is the System Administrator */
  21526.         or o.uid = user_id()     /* User created the object */
  21527.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21528.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21529.             from sysprotects p
  21530.             /* join to correlate with all rows in sysobjects */
  21531.             where p.id = o.id
  21532.             /* get rows for public,current user,user's group */
  21533.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21534.             /* check for SELECT,EXECUTE privilege */
  21535.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21536.             ) = 1    /* final magic...compare Grants      */
  21537.         )        
  21538.     order by 4, 2, 3
  21539. go
  21540. dump tran master with no_log
  21541. go
  21542.  
  21543. if (charindex('7.00', @@version) > 0 or
  21544.     charindex('8.00', @@version) > 0)
  21545.     drop procedure sp_tables_rowset
  21546. else
  21547. begin
  21548.     print ''
  21549.     print ''
  21550.     print 'Warning:'
  21551.     print 'you are installing the stored procedures '
  21552.     print 'on a pre 8.0 SQL Server.'
  21553.     print 'Ignore the following errors.'
  21554. end
  21555. go
  21556.  
  21557. /*    Procedure for 8.0 server */
  21558. create procedure sp_tables_rowset
  21559.     (
  21560.     @table_name    sysname, 
  21561.     @table_schema    sysname = null,    
  21562.     @table_type    nvarchar(255) = null 
  21563.     )
  21564. as
  21565.     select    *
  21566.     from    (select    TABLE_CATALOG    = db_name(),
  21567.             TABLE_SCHEMA    = user_name(uid),
  21568.             TABLE_NAME    = name,
  21569.             TABLE_TYPE    = convert(nvarchar(30),
  21570.                         case type 
  21571.                         when 'U' then 
  21572.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21573.                             then N'TABLE' else N'SYSTEM TABLE' end
  21574.                         when 'S' then N'SYSTEM TABLE'
  21575.                         when 'V' then 
  21576.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21577.                             then N'VIEW' else N'SYSTEM VIEW' end
  21578.                         end),
  21579.             TABLE_GUID    = convert(uniqueidentifier, null),
  21580.             DESCRIPTION    = convert(nvarchar(1), null),
  21581.             TABLE_PROPID    = convert(int,null),
  21582.             DATE_CREATED    = crdate,
  21583.             DATE_MODIFIED    = convert(datetime,null)
  21584.         from    sysobjects
  21585.         where    name = @table_name
  21586.         and    type in ('U','V','S')
  21587.         and    permissions(id) <> 0
  21588.         ) as o
  21589.  
  21590.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21591.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  21592.     order by 4, 2, 3
  21593. go
  21594. dump tran master with no_log
  21595. go
  21596. create procedure sp_tables_rowset;2
  21597.     (
  21598.     @table_schema    sysname = null,    
  21599.     @table_type    nvarchar(255) = null 
  21600.     )
  21601. as
  21602.     select    *
  21603.     from    (select    TABLE_CATALOG    = db_name(),
  21604.             TABLE_SCHEMA    = user_name(uid),
  21605.             TABLE_NAME    = name,
  21606.             TABLE_TYPE    = convert(nvarchar(30),
  21607.                         case type 
  21608.                         when 'U' then 
  21609.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21610.                             then N'TABLE' else N'SYSTEM TABLE' end
  21611.                         when 'S' then N'SYSTEM TABLE'
  21612.                         when 'V' then 
  21613.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21614.                             then N'VIEW' else N'SYSTEM VIEW' end
  21615.                         end),
  21616.             TABLE_GUID    = convert(uniqueidentifier, null),
  21617.             DESCRIPTION    = convert(nvarchar(1), null),
  21618.             TABLE_PROPID    = convert(int,null),
  21619.             DATE_CREATED    = crdate,
  21620.             DATE_MODIFIED    = convert(datetime,null)
  21621.         from    sysobjects
  21622.         where    type in ('U','V','S')
  21623.         and    permissions(id) <> 0
  21624.         ) as o
  21625.  
  21626.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21627.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  21628.     order by 4, 2, 3
  21629. go
  21630. dump tran master with no_log
  21631. go
  21632. create procedure sp_tables_rowset;5
  21633.     (
  21634.     @table_server        sysname,
  21635.     @table_catalog        sysname = null,
  21636.     @table_name        sysname = null,
  21637.     @table_schema        sysname = null,
  21638.     @table_type        sysname = null
  21639.     )
  21640. as
  21641.     select
  21642.         TABLE_CATALOG,
  21643.         TABLE_SCHEMA,
  21644.         TABLE_NAME,
  21645.         TABLE_TYPE,
  21646.         TABLE_GUID,
  21647.         DESCRIPTION
  21648.     --    TABLE_PROPID,
  21649.     --    DATE_CREATED,
  21650.     --    DATE_MODIFIED
  21651.     from master.dbo.SYSREMOTE_TABLES <
  21652.                 @table_server,
  21653.                 @table_catalog,
  21654.                 @table_schema,
  21655.                 @table_name,
  21656.                 @table_type >
  21657.     order by 4,1,2,3
  21658. go
  21659.  
  21660. grant execute on sp_tables_rowset to public
  21661. go
  21662.  
  21663. dump tran master with no_log
  21664. go
  21665. if (charindex('6.00', @@version) > 0)
  21666.     begin
  21667.     if (exists (select * from sysobjects
  21668.             where name = 'sp_tables_rowset' and type = 'P '))
  21669.         begin
  21670.         drop procedure sp_tables_rowset
  21671.         dump tran master with no_log
  21672.         end
  21673.     end
  21674. go
  21675.  
  21676.  
  21677.  
  21678. print ''
  21679. print 'creating sp_tables_info_rowset'
  21680. go
  21681.  
  21682.  
  21683. /*    Procedure for 6.50 and earlier servers */
  21684. create procedure sp_tables_info_rowset
  21685.     (
  21686.     @table_name    varchar(255), 
  21687.     @table_schema    varchar(255) = null,    
  21688.     @table_type    varchar(255) = null 
  21689.     )
  21690. as
  21691.     select    TABLE_CATALOG        = db_name(),
  21692.         TABLE_SCHEMA        = user_name(o.uid),
  21693.         TABLE_NAME        = o.name,
  21694.         TABLE_TYPE        = convert(varchar(30),
  21695.                         case o.type 
  21696.                         when 'U' then 'TABLE'
  21697.                         when 'V' then 'VIEW'
  21698.                         when 'S' then 'SYSTEM TABLE'
  21699.                         end),
  21700.         TABLE_GUID        = convert(binary(16), null),
  21701.         BOOKMARKS        = convert(bit, 1),
  21702.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21703.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21704.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21705.         BOOKMARK_INFORMATION    = convert(int, 0),
  21706.         TABLE_VERSION        = convert(int, o.schema_ver),
  21707.         CARDINALITY        = x.rows,
  21708.         DESCRIPTION        = convert(varchar(1), null),
  21709.         TABLE_PROPID        = convert(int, null)
  21710.  
  21711.     from    sysusers u, 
  21712.         sysobjects o,
  21713.         sysindexes x
  21714.  
  21715.     where    o.type in ('U','V','S')
  21716.     and     o.name = @table_name
  21717.     and     (    @table_schema is null
  21718.         or    @table_schema = user_name(o.uid)
  21719.         )
  21720.     and     (
  21721.             @table_type is null
  21722.         or    @table_type = case o.type 
  21723.                     when 'U' then 'TABLE'
  21724.                     when 'V' then 'VIEW'
  21725.                     when 'S' then 'SYSTEM TABLE'
  21726.                     end
  21727.         )
  21728.     and    o.id *= x.id
  21729.     and    x.indid in (0,1)
  21730.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21731.     and     (
  21732.         suser_id() = 1     /* User is the System Administrator */
  21733.         or o.uid = user_id()     /* User created the object */
  21734.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21735.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21736.             from sysprotects p
  21737.             /* join to correlate with all rows in sysobjects */
  21738.             where p.id = o.id
  21739.             /* get rows for public,current user,user's group */
  21740.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21741.             /* check for SELECT,EXECUTE privilege */
  21742.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21743.             ) = 1    /* final magic...compare Grants      */
  21744.         )        
  21745.     order by 4, 2, 3
  21746. go
  21747. dump tran master with no_log
  21748. go
  21749. create procedure sp_tables_info_rowset;2
  21750.     (
  21751.     @table_schema    varchar(255) = null,    
  21752.     @table_type    varchar(255) = null 
  21753.     )
  21754. as
  21755.     select    TABLE_CATALOG        = db_name(),
  21756.         TABLE_SCHEMA        = user_name(o.uid),
  21757.         TABLE_NAME        = o.name,
  21758.         TABLE_TYPE        = convert(varchar(30),
  21759.                         case o.type 
  21760.                         when 'U' then 'TABLE'
  21761.                         when 'V' then 'VIEW'
  21762.                         when 'S' then 'SYSTEM TABLE'
  21763.                         end),
  21764.         TABLE_GUID        = convert(binary(16), null),
  21765.         BOOKMARKS        = convert(bit, 1),
  21766.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21767.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21768.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21769.         BOOKMARK_INFORMATION    = convert(int, 0),
  21770.         TABLE_VERSION        = convert(int, o.schema_ver),
  21771.         CARDINALITY        = x.rows,
  21772.         DESCRIPTION        = convert(varchar(1), null),
  21773.         TABLE_PROPID        = convert(int, null)
  21774.  
  21775.     from    sysusers u, 
  21776.         sysobjects o,
  21777.         sysindexes x
  21778.  
  21779.     where    o.type in ('U','V','S')
  21780.     and     (    @table_schema is null
  21781.         or    @table_schema = user_name(o.uid)
  21782.         )
  21783.     and     (
  21784.             @table_type is null
  21785.         or    @table_type = case o.type 
  21786.                     when 'U' then 'TABLE'
  21787.                     when 'V' then 'VIEW'
  21788.                     when 'S' then 'SYSTEM TABLE'
  21789.                     end
  21790.         )
  21791.     and    o.id *= x.id
  21792.     and    x.indid in (0,1)
  21793.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21794.     and     (
  21795.         suser_id() = 1     /* User is the System Administrator */
  21796.         or o.uid = user_id()     /* User created the object */
  21797.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21798.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21799.             from sysprotects p
  21800.             /* join to correlate with all rows in sysobjects */
  21801.             where p.id = o.id
  21802.             /* get rows for public,current user,user's group */
  21803.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21804.             /* check for SELECT,EXECUTE privilege */
  21805.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21806.             ) = 1    /* final magic...compare Grants      */
  21807.         )        
  21808.     order by 4, 2, 3
  21809. go
  21810. dump tran master with no_log
  21811. go
  21812.  
  21813. if (charindex('7.00', @@version) > 0 or
  21814.     charindex('8.00', @@version) > 0)
  21815.     drop procedure sp_tables_info_rowset
  21816. else
  21817. begin
  21818.     print ''
  21819.     print ''
  21820.     print 'Warning:'
  21821.     print 'you are installing the stored procedures '
  21822.     print 'on a pre 7.0 SQL Server.'
  21823.     print 'Ignore the following errors.'
  21824. end
  21825. go
  21826.  
  21827. /*    Procedure for 7.0 server */
  21828. create procedure sp_tables_info_rowset
  21829.     (
  21830.     @table_name    sysname, 
  21831.     @table_schema    sysname = null,    
  21832.     @table_type    nvarchar(255) = null 
  21833.     )
  21834. as
  21835.     select    *
  21836.     from    (select    TABLE_CATALOG        = db_name(),
  21837.             TABLE_SCHEMA        = user_name(o.uid),
  21838.             TABLE_NAME        = o.name,
  21839.             TABLE_TYPE        = convert(nvarchar(30),
  21840.                             case o.type 
  21841.                             when 'U' then 
  21842.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21843.                                 then N'TABLE' else N'SYSTEM TABLE' end
  21844.                             when 'S' then N'SYSTEM TABLE'
  21845.                             when 'V' then 
  21846.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21847.                                 then N'VIEW' else N'SYSTEM VIEW' end
  21848.                             end),
  21849.             TABLE_GUID        = convert(uniqueidentifier, null),
  21850.             BOOKMARKS        = convert(bit, 1),
  21851.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21852.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21853.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21854.             BOOKMARK_INFORMATION    = convert(int, 0),
  21855.             TABLE_VERSION        = convert(int, o.schema_ver),
  21856.             CARDINALITY        = x.rows,
  21857.             DESCRIPTION        = convert(nvarchar(1), null),
  21858.             TABLE_PROPID        = convert(int, null) 
  21859.     
  21860.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  21861.         where    o.name = @table_name
  21862.         and     o.type in ('U','V','S')
  21863.         and    permissions(o.id) <> 0) as t
  21864.  
  21865.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21866.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  21867.     order by 4, 2, 3
  21868. go
  21869. dump tran master with no_log
  21870. go
  21871. create procedure sp_tables_info_rowset;2
  21872.     (
  21873.     @table_schema    sysname = null,    
  21874.     @table_type    nvarchar(255) = null 
  21875.     )
  21876. as
  21877.     select    *
  21878.     from    (select    TABLE_CATALOG        = db_name(),
  21879.             TABLE_SCHEMA        = user_name(o.uid),
  21880.             TABLE_NAME        = o.name,
  21881.             TABLE_TYPE        = convert(nvarchar(30),
  21882.                             case o.type 
  21883.                             when 'U' then 
  21884.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21885.                                 then N'TABLE' else N'SYSTEM TABLE' end
  21886.                             when 'S' then N'SYSTEM TABLE'
  21887.                             when 'V' then 
  21888.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21889.                                 then N'VIEW' else N'SYSTEM VIEW' end
  21890.                             end),
  21891.             TABLE_GUID        = convert(uniqueidentifier, null),
  21892.             BOOKMARKS        = convert(bit, 1),
  21893.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21894.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21895.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21896.             BOOKMARK_INFORMATION    = convert(int, 0),
  21897.             TABLE_VERSION        = convert(int, o.schema_ver),
  21898.             CARDINALITY        = x.rows,
  21899.             DESCRIPTION        = convert(nvarchar(1), null),
  21900.             TABLE_PROPID        = convert(int, null) 
  21901.     
  21902.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  21903.         where     o.type in ('U','V','S')
  21904.         and    permissions(o.id) <> 0) as t
  21905.  
  21906.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21907.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  21908.     order by 4, 2, 3
  21909. go
  21910.  
  21911. dump tran master with no_log
  21912. go
  21913.  
  21914. if (charindex('8.00', @@version) > 0)
  21915.     drop procedure sp_tables_info_rowset
  21916. else
  21917. begin
  21918.     print ''
  21919.     print ''
  21920.     print 'Warning:'
  21921.     print 'you are installing the stored procedures '
  21922.     print 'on a pre 8.0 SQL Server.'
  21923.     print 'Ignore the following errors.'
  21924. end
  21925. go
  21926.  
  21927. /*    Procedure for 8.0 server */
  21928. create procedure sp_tables_info_rowset
  21929.     (
  21930.     @table_name    sysname, 
  21931.     @table_schema    sysname = null,    
  21932.     @table_type    nvarchar(255) = null 
  21933.     )
  21934. as
  21935.     select    *
  21936.     from    (select    TABLE_CATALOG        = db_name(),
  21937.             TABLE_SCHEMA        = user_name(o.uid),
  21938.             TABLE_NAME        = o.name,
  21939.             TABLE_TYPE        = convert(nvarchar(30),
  21940.                             case o.type 
  21941.                             when 'U' then 
  21942.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21943.                                 then N'TABLE' else N'SYSTEM TABLE' end
  21944.                             when 'S' then N'SYSTEM TABLE'
  21945.                             when 'V' then 
  21946.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21947.                                 then N'VIEW' else N'SYSTEM VIEW' end
  21948.                             end),
  21949.             TABLE_GUID        = convert(uniqueidentifier, null),
  21950.             BOOKMARKS        = convert(bit, 1),
  21951.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21952.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21953.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21954.             BOOKMARK_INFORMATION    = convert(int, 0),
  21955.             TABLE_VERSION        = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  21956.             CARDINALITY        = x.rows,
  21957.             DESCRIPTION        = convert(nvarchar(1), null),
  21958.             TABLE_PROPID        = convert(int, null) 
  21959.     
  21960.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  21961.         where    o.name = @table_name
  21962.         and     o.type in ('U','V','S')
  21963.         and    permissions(o.id) <> 0) as t
  21964.  
  21965.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21966.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  21967.     order by 4, 2, 3
  21968. go
  21969. dump tran master with no_log
  21970. go
  21971. create procedure sp_tables_info_rowset;2
  21972.     (
  21973.     @table_schema    sysname = null,    
  21974.     @table_type    nvarchar(255) = null 
  21975.     )
  21976. as
  21977.     select    *
  21978.     from    (select    TABLE_CATALOG        = db_name(),
  21979.             TABLE_SCHEMA        = user_name(o.uid),
  21980.             TABLE_NAME        = o.name,
  21981.             TABLE_TYPE        = convert(nvarchar(30),
  21982.                             case o.type 
  21983.                             when 'U' then 
  21984.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21985.                                 then N'TABLE' else N'SYSTEM TABLE' end
  21986.                             when 'S' then N'SYSTEM TABLE'
  21987.                             when 'V' then 
  21988.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21989.                                 then N'VIEW' else N'SYSTEM VIEW' end
  21990.                             end),
  21991.             TABLE_GUID        = convert(uniqueidentifier, null),
  21992.             BOOKMARKS        = convert(bit, 1),
  21993.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21994.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21995.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21996.             BOOKMARK_INFORMATION    = convert(int, 0),
  21997.             TABLE_VERSION        = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  21998.             CARDINALITY        = x.rows,
  21999.             DESCRIPTION        = convert(nvarchar(1), null),
  22000.             TABLE_PROPID        = convert(int, null) 
  22001.     
  22002.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22003.         where     o.type in ('U','V','S')
  22004.         and    permissions(o.id) <> 0) as t
  22005.  
  22006.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22007.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22008.     order by 4, 2, 3
  22009. go
  22010.  
  22011. grant execute on sp_tables_info_rowset to public
  22012. go
  22013.  
  22014. dump tran master with no_log
  22015. go
  22016.  
  22017. if (charindex('6.00', @@version) > 0)
  22018.     begin
  22019.     if (exists (select * from sysobjects
  22020.         where name = 'sp_tables_info_rowset' and type = 'P '))
  22021.         begin
  22022.         drop procedure sp_tables_info_rowset
  22023.         dump tran master with no_log
  22024.         end
  22025.     end
  22026. go
  22027.  
  22028. print ''
  22029. print 'creating sp_table_constraints_rowset'
  22030. go
  22031.  
  22032.  
  22033. /*    Procedure for 6.50 and earlier servers */
  22034. create procedure sp_table_constraints_rowset
  22035.     (
  22036.     @table_name            varchar(255), 
  22037.     @table_schema        varchar(255) = null,
  22038.     @table_catalog        varchar(255) = null,
  22039.     @constraint_name    varchar(255) = null,
  22040.     @constraint_schema    varchar(255) = null,
  22041.     @constraint_catalog    varchar(255) = null,
  22042.     @constraint_type    varchar(255) = null 
  22043.     )
  22044. as
  22045.     select
  22046.         CONSTRAINT_CATALOG    = db_name(),
  22047.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22048.         CONSTRAINT_NAME        = c_obj.name,
  22049.         TABLE_CATALOG        = db_name(),
  22050.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22051.         TABLE_NAME            = t_obj.name,
  22052.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22053.                                 when 1 then 'PRIMARY KEY'
  22054.                                 when 2 then    'UNIQUE'
  22055.                                 when 3 then    'FOREIGN KEY'
  22056.                                 when 4 then    'CHECK'
  22057.                                  end, 
  22058.         IS_DEFERRABLE        = convert(tinyint, 0),
  22059.         INITIALLY_DEFERRED    = convert(tinyint, 0),
  22060.         DESCRIPTION            = convert(varchar(1), null)
  22061.  
  22062.     from 
  22063.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22064.     where
  22065.             t_obj.name    = @table_name
  22066.         and t_obj.type in ('U','S')
  22067.         and (@table_catalog is null or @table_catalog = db_name())
  22068.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22069.         and c.id = t_obj.id
  22070.         and (c.status & 0xf) between 1 and 4
  22071.         and c_obj.id    = c.constid
  22072.         and    c_obj.uid    = user_id()
  22073.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22074.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22075.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22076.         and (@constraint_type is null
  22077.             or (c.status & 0xf)
  22078.                 = case @constraint_type
  22079.                     when 'PRIMARY KEY' then 1
  22080.                     when 'UNIQUE' then 2
  22081.                     when 'FOREIGN KEY' then 3
  22082.                     when 'CHECK' then 4
  22083.                     end)
  22084. order by 2,3,5,6,7 
  22085. go
  22086. dump tran master with no_log
  22087. go
  22088. create procedure sp_table_constraints_rowset;2
  22089.     (
  22090.     @table_schema        varchar(255) = null,
  22091.     @table_catalog        varchar(255) = null,
  22092.     @constraint_name    varchar(255) = null,
  22093.     @constraint_schema    varchar(255) = null,
  22094.     @constraint_catalog    varchar(255) = null,
  22095.     @constraint_type    varchar(255) = null 
  22096.     )
  22097. as
  22098.     select
  22099.         CONSTRAINT_CATALOG    = db_name(),
  22100.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22101.         CONSTRAINT_NAME        = c_obj.name,
  22102.         TABLE_CATALOG        = db_name(),
  22103.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22104.         TABLE_NAME            = t_obj.name,
  22105.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22106.                                 when 1 then 'PRIMARY KEY'
  22107.                                 when 2 then    'UNIQUE'
  22108.                                 when 3 then    'FOREIGN KEY'
  22109.                                 when 4 then    'CHECK'
  22110.                                  end, 
  22111.         IS_DEFERRABLE        = convert(tinyint, 0),
  22112.         INITIALLY_DEFERRED    = convert(tinyint, 0),
  22113.         DESCRIPTION            = convert(varchar(1), null)
  22114.  
  22115.     from 
  22116.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22117.     where
  22118.             t_obj.type in ('U','S')
  22119.         and (@table_catalog is null or @table_catalog = db_name())
  22120.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22121.         and c.id = t_obj.id
  22122.         and (c.status & 0xf) between 1 and 4
  22123.         and c_obj.id    = c.constid
  22124.         and    c_obj.uid    = user_id()
  22125.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22126.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22127.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22128.         and (@constraint_type is null
  22129.             or (c.status & 0xf)
  22130.                 = case @constraint_type
  22131.                     when 'PRIMARY KEY' then 1
  22132.                     when 'UNIQUE' then 2
  22133.                     when 'FOREIGN KEY' then 3
  22134.                     when 'CHECK' then 4
  22135.                     end)
  22136. order by 2,3,5,6,7 
  22137. go
  22138. dump tran master with no_log
  22139. go
  22140.  
  22141.  
  22142. if (charindex('7.00', @@version) > 0 or
  22143.     charindex('8.00', @@version) > 0)
  22144.     drop procedure sp_table_constraints_rowset
  22145. else
  22146. begin
  22147.     print ''
  22148.     print ''
  22149.     print 'Warning:'
  22150.     print 'you are installing the stored procedures '
  22151.     print 'on a pre 8.0 SQL Server.'
  22152.     print 'Ignore the following errors.'
  22153. end
  22154. go
  22155.  
  22156. /*    Procedure for 8.0 server */
  22157. create procedure sp_table_constraints_rowset
  22158.     (
  22159.     @table_name            sysname, 
  22160.     @table_schema        sysname = null,
  22161.     @table_catalog        sysname = null,
  22162.     @constraint_name    sysname = null,
  22163.     @constraint_schema    sysname = null,
  22164.     @constraint_catalog    sysname = null,
  22165.     @constraint_type    nvarchar(255) = null 
  22166.     )
  22167. as
  22168.     select
  22169.         CONSTRAINT_CATALOG    = db_name(),
  22170.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22171.         CONSTRAINT_NAME        = c_obj.name,
  22172.         TABLE_CATALOG        = db_name(),
  22173.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22174.         TABLE_NAME            = t_obj.name,
  22175.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22176.                                 when 1 then N'PRIMARY KEY'
  22177.                                 when 2 then    N'UNIQUE'
  22178.                                 when 3 then    N'FOREIGN KEY'
  22179.                                 when 4 then    N'CHECK'
  22180.                                  end, 
  22181.         IS_DEFERRABLE        = convert(bit, 0),
  22182.         INITIALLY_DEFERRED    = convert(bit, 0),
  22183.         DESCRIPTION            = convert(nvarchar(1), null)
  22184.  
  22185.     from 
  22186.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22187.     where
  22188.             t_obj.name    = @table_name
  22189.         and t_obj.type in ('U','S')
  22190.         and (@table_catalog is null or @table_catalog = db_name())
  22191.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22192.         and c.id = t_obj.id
  22193.         and (c.status & 0xf) between 1 and 4
  22194.         and c_obj.id    = c.constid
  22195.         and    c_obj.uid    = user_id()
  22196.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22197.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22198.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22199.         and (@constraint_type is null
  22200.             or (c.status & 0xf)
  22201.                 = case @constraint_type
  22202.                     when N'PRIMARY KEY' then 1
  22203.                     when N'UNIQUE' then 2
  22204.                     when N'FOREIGN KEY' then 3
  22205.                     when N'CHECK' then 4
  22206.                     end)
  22207. order by 2,3,5,6,7 
  22208. go
  22209. dump tran master with no_log
  22210. go
  22211. create procedure sp_table_constraints_rowset;2
  22212.     (
  22213.     @table_schema        sysname = null,
  22214.     @table_catalog        sysname = null,
  22215.     @constraint_name    sysname = null,
  22216.     @constraint_schema    sysname = null,
  22217.     @constraint_catalog    sysname = null,
  22218.     @constraint_type    nvarchar(255) = null 
  22219.     )
  22220. as
  22221.     select
  22222.         CONSTRAINT_CATALOG    = db_name(),
  22223.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22224.         CONSTRAINT_NAME        = c_obj.name,
  22225.         TABLE_CATALOG        = db_name(),
  22226.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22227.         TABLE_NAME            = t_obj.name,
  22228.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22229.                                 when 1 then N'PRIMARY KEY'
  22230.                                 when 2 then    N'UNIQUE'
  22231.                                 when 3 then    N'FOREIGN KEY'
  22232.                                 when 4 then    N'CHECK'
  22233.                                  end, 
  22234.         IS_DEFERRABLE        = convert(bit, 0),
  22235.         INITIALLY_DEFERRED    = convert(bit, 0),
  22236.         DESCRIPTION            = convert(nvarchar(1), null)
  22237.  
  22238.     from 
  22239.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22240.     where
  22241.             t_obj.type in ('U','S')
  22242.         and (@table_catalog is null or @table_catalog = db_name())
  22243.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22244.         and c.id = t_obj.id
  22245.         and (c.status & 0xf) between 1 and 4
  22246.         and c_obj.id    = c.constid
  22247.         and    c_obj.uid    = user_id()
  22248.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22249.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22250.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22251.         and (@constraint_type is null
  22252.             or (c.status & 0xf)
  22253.                 = case @constraint_type
  22254.                     when N'PRIMARY KEY' then 1
  22255.                     when N'UNIQUE' then 2
  22256.                     when N'FOREIGN KEY' then 3
  22257.                     when N'CHECK' then 4
  22258.                     end)
  22259. order by 2,3,5,6,7 
  22260. go
  22261. dump tran master with no_log
  22262.  
  22263.  
  22264. grant execute on sp_table_constraints_rowset to public
  22265. go
  22266.  
  22267. dump tran master with no_log
  22268. go
  22269. if (charindex('6.00', @@version) > 0)
  22270.     begin
  22271.     if (exists (select * from sysobjects
  22272.             where name = 'sp_table_constraints_rowset' and type = 'P '))
  22273.         begin
  22274.         drop procedure sp_table_constraints_rowset
  22275.         dump tran master with no_log
  22276.         end
  22277.     end
  22278. go
  22279.  
  22280. print ''
  22281. print 'creating sp_table_privileges_rowset'
  22282. go
  22283.  
  22284. /*    Procedure for 6.0 and 6.5 server */
  22285. CREATE PROCEDURE sp_table_privileges_rowset
  22286.     (
  22287.     @table_name    varchar(255) = null,
  22288.     @table_schema    varchar(255) = null,
  22289.     @grantor    varchar(255) = null,
  22290.     @grantee    varchar(255) = null
  22291.     )
  22292. as
  22293. IF @table_name is not null
  22294.     BEGIN
  22295.     select
  22296.         GRANTOR        = user_name(p.grantor),
  22297.         GRANTEE        = user_name(u.uid),
  22298.         TABLE_CATALOG    = db_name(),
  22299.         TABLE_SCHEMA    = user_name(o.uid),
  22300.         TABLE_NAME    = o.name,
  22301.         PRIVILEGE_TYPE    = convert(varchar(30),
  22302.                     case p.action
  22303.                     when 193 then 'SELECT'
  22304.                     when 195 then 'INSERT'
  22305.                     when 196 then 'DELETE'
  22306.                     when 197 then 'UPDATE'
  22307.                     else 'REFERENCES'
  22308.                     end),
  22309.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22310.     from 
  22311.         sysprotects p, sysobjects o, sysusers u
  22312.     where
  22313.         o.name = @table_name
  22314.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22315.     and    (@grantee is null or @grantee = user_name(u.uid))
  22316.     and     o.type in ('U','V','S')
  22317.     and    p.id = o.id
  22318.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22319.             /* expand groups */
  22320.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22321.     and     p.protecttype <> 206    /* only grant rows */
  22322.     and     p.action in (26,193,195,196,197)
  22323.     and     o.uid <> u.uid            /* no rows for owner */
  22324.     and     not exists (            /* exclude revoke'd privileges */
  22325.             select     *
  22326.             from     sysprotects p1
  22327.             where    p1.protecttype = 206
  22328.             and     p1.action = p.action
  22329.             and     p1.id = p.id
  22330.             and     p1.uid = u.uid)
  22331.     union
  22332.     select    /*    Add rows for table owner */
  22333.         GRANTOR        = user_name(u.uid),
  22334.         GRANTEE        = user_name(o.uid),
  22335.         TABLE_CATALOG    = db_name(),
  22336.         TABLE_SCHEMA    = user_name(o.uid),
  22337.         TABLE_NAME    = o.name,
  22338.         PRIVILEGE_TYPE    = convert(varchar(30),
  22339.                     case v.number
  22340.                     when 193 then 'SELECT'
  22341.                     when 195 then 'INSERT'
  22342.                     when 196 then 'DELETE'
  22343.                     when 197 then 'UPDATE'
  22344.                     else 'REFERENCES'
  22345.                     end),
  22346.         IS_GRANTABLE    = convert(bit,1)    
  22347.     from 
  22348.         sysobjects o, master.dbo.spt_values v, sysusers u
  22349.     where
  22350.         o.name = @table_name
  22351.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22352.     and    (@grantee is null or @grantee = user_name(o.uid))
  22353.     and     o.type in ('U','V','S')
  22354.     and     u.uid = 1        /* grantor is dbo of database */
  22355.     and    (@grantor is null or @grantor = user_name(u.uid))
  22356.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22357.     and     v.number in (26,193,195,196,197)
  22358.     and     not exists (    /* exclude revoke'd privileges */
  22359.             select     *
  22360.             from     sysprotects p1
  22361.             where    p1.protecttype = 206
  22362.             and     p1.action = v.number
  22363.             and     p1.id = o.id
  22364.             and     p1.uid = o.uid)
  22365.     order by 4,5,6,1,2
  22366.     END
  22367. ELSE
  22368.     BEGIN
  22369.     select
  22370.         GRANTOR        = user_name(p.grantor),
  22371.         GRANTEE        = user_name(u.uid),
  22372.         TABLE_CATALOG    = db_name(),
  22373.         TABLE_SCHEMA    = user_name(o.uid),
  22374.         TABLE_NAME    = o.name,
  22375.         PRIVILEGE_TYPE    = convert(varchar(30),
  22376.                     case p.action
  22377.                     when 193 then 'SELECT'
  22378.                     when 195 then 'INSERT'
  22379.                     when 196 then 'DELETE'
  22380.                     when 197 then 'UPDATE'
  22381.                     else 'REFERENCES'
  22382.                     end),
  22383.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22384.     from 
  22385.         sysprotects p, sysobjects o, sysusers u
  22386.     where
  22387.         o.type in ('U','V','S')
  22388.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22389.     and    (@grantee is null or @grantee = user_name(u.uid))
  22390.     and    p.id = o.id
  22391.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22392.             /* expand groups */
  22393.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22394.     and     p.protecttype <> 206    /* only grant rows */
  22395.     and     p.action in (26,193,195,196,197)
  22396.     and     o.uid <> u.uid            /* no rows for owner */
  22397.     and     not exists (            /* exclude revoke'd privileges */
  22398.             select     *
  22399.             from     sysprotects p1
  22400.             where    p1.protecttype = 206
  22401.             and     p1.action = p.action
  22402.             and     p1.id = p.id
  22403.             and     p1.uid = u.uid)
  22404.     union
  22405.     select    /*    Add rows for table owner */
  22406.         GRANTOR        = user_name(u.uid),
  22407.         GRANTEE        = user_name(o.uid),
  22408.         TABLE_CATALOG    = db_name(),
  22409.         TABLE_SCHEMA    = user_name(o.uid),
  22410.         TABLE_NAME    = o.name,
  22411.         PRIVILEGE_TYPE    = convert(varchar(30),
  22412.                     case v.number
  22413.                     when 193 then 'SELECT'
  22414.                     when 195 then 'INSERT'
  22415.                     when 196 then 'DELETE'
  22416.                     when 197 then 'UPDATE'
  22417.                     else 'REFERENCES'
  22418.                     end),
  22419.         IS_GRANTABLE    = convert(bit,1)    
  22420.     from 
  22421.         sysobjects o, master.dbo.spt_values v, sysusers u
  22422.     where
  22423.         o.type in ('U','V','S')
  22424.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22425.     and    (@grantee is null or @grantee = user_name(o.uid))
  22426.     and     u.uid = 1        /* grantor is dbo of database */
  22427.     and    (@grantor is null or @grantor = user_name(u.uid))
  22428.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22429.     and     v.number in (26,193,195,196,197)
  22430.     and     not exists (    /* exclude revoke'd privileges */
  22431.             select     *
  22432.             from     sysprotects p1
  22433.             where    p1.protecttype = 206
  22434.             and     p1.action = v.number
  22435.             and     p1.id = o.id
  22436.             and     p1.uid = o.uid)
  22437.     order by 4,5,6,1,2
  22438.     END
  22439. go
  22440. dump tran master with no_log
  22441. go
  22442. CREATE PROCEDURE sp_table_privileges_rowset;2
  22443.     (
  22444.        @handle        int output,
  22445.        @scrollopt    int output,
  22446.     @ccopt        int output,
  22447.     @rows        int output,
  22448.     @table_name     varchar(255) = null,
  22449.     @table_schema    varchar(255) = null,
  22450.     @grantor    varchar(255) = null,
  22451.     @grantee    varchar(255) = null
  22452.     )
  22453. as
  22454. declare @ret int
  22455. SET NOCOUNT ON
  22456. create table #sptprivsrowset1
  22457.     (
  22458.     GRANTOR        sysname not null,
  22459.     GRANTEE        sysname not null,
  22460.     TABLE_CATALOG    sysname not null,
  22461.     TABLE_SCHEMA    sysname not null,
  22462.     TABLE_NAME    sysname not null,
  22463.     PRIVILEGE_TYPE    sysname not null,
  22464.     IS_GRANTABLE    bit not null
  22465.     )
  22466.     
  22467. IF @table_name is not null
  22468.     BEGIN
  22469.     insert into #sptprivsrowset1
  22470.     select
  22471.         GRANTOR        = user_name(p.grantor),
  22472.         GRANTEE        = user_name(u.uid),
  22473.         TABLE_CATALOG    = db_name(),
  22474.         TABLE_SCHEMA    = user_name(o.uid),
  22475.         TABLE_NAME    = o.name,
  22476.         PRIVILEGE_TYPE    = convert(varchar(30),
  22477.                     case p.action
  22478.                     when 193 then 'SELECT'
  22479.                     when 195 then 'INSERT'
  22480.                     when 196 then 'DELETE'
  22481.                     when 197 then 'UPDATE'
  22482.                     else 'REFERENCES'
  22483.                     end),
  22484.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22485.     from 
  22486.         sysprotects p, sysobjects o, sysusers u
  22487.     where
  22488.         o.name = @table_name
  22489.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22490.     and    (@grantee is null or @grantee = user_name(u.uid))
  22491.     and     o.type in ('U','V','S')
  22492.     and    p.id = o.id
  22493.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22494.             /* expand groups */
  22495.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22496.     and     p.protecttype <> 206    /* only grant rows */
  22497.     and     p.action in (26,193,195,196,197)
  22498.     and     o.uid <> u.uid            /* no rows for owner */
  22499.     and     not exists (            /* exclude revoke'd privileges */
  22500.             select     *
  22501.             from     sysprotects p1
  22502.             where    p1.protecttype = 206
  22503.             and     p1.action = p.action
  22504.             and     p1.id = p.id
  22505.             and     p1.uid = u.uid)
  22506.     union
  22507.     select    /*    Add rows for table owner */
  22508.         GRANTOR        = user_name(u.uid),
  22509.         GRANTEE        = user_name(o.uid),
  22510.         TABLE_CATALOG    = db_name(),
  22511.         TABLE_SCHEMA    = user_name(o.uid),
  22512.         TABLE_NAME    = o.name,
  22513.         PRIVILEGE_TYPE    = convert(varchar(30),
  22514.                     case v.number
  22515.                     when 193 then 'SELECT'
  22516.                     when 195 then 'INSERT'
  22517.                     when 196 then 'DELETE'
  22518.                     when 197 then 'UPDATE'
  22519.                     else 'REFERENCES'
  22520.                     end),
  22521.         IS_GRANTABLE    = convert(bit,1)    
  22522.     from 
  22523.         sysobjects o, master.dbo.spt_values v, sysusers u
  22524.     where
  22525.         o.name = @table_name
  22526.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22527.     and    (@grantee is null or @grantee = user_name(o.uid))
  22528.     and     o.type in ('U','V','S')
  22529.     and     u.uid = 1        /* grantor is dbo of database */
  22530.     and    (@grantor is null or @grantor = user_name(u.uid))
  22531.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22532.     and     v.number in (26,193,195,196,197)
  22533.     and     not exists (    /* exclude revoke'd privileges */
  22534.             select     *
  22535.             from     sysprotects p1
  22536.             where    p1.protecttype = 206
  22537.             and     p1.action = v.number
  22538.             and     p1.id = o.id
  22539.             and     p1.uid = o.uid)
  22540.     order by 4,5,6,1,2
  22541.     END
  22542. ELSE
  22543.     BEGIN
  22544.     insert into #sptprivsrowset1
  22545.     select
  22546.         GRANTOR        = user_name(p.grantor),
  22547.         GRANTEE        = user_name(u.uid),
  22548.         TABLE_CATALOG    = db_name(),
  22549.         TABLE_SCHEMA    = user_name(o.uid),
  22550.         TABLE_NAME    = o.name,
  22551.         PRIVILEGE_TYPE    = convert(varchar(30),
  22552.                     case p.action
  22553.                     when 193 then 'SELECT'
  22554.                     when 195 then 'INSERT'
  22555.                     when 196 then 'DELETE'
  22556.                     when 197 then 'UPDATE'
  22557.                     else 'REFERENCES'
  22558.                     end),
  22559.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22560.     from 
  22561.         sysprotects p, sysobjects o, sysusers u
  22562.     where
  22563.         o.type in ('U','V','S')
  22564.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22565.     and    (@grantee is null or @grantee = user_name(u.uid))
  22566.     and    p.id = o.id
  22567.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22568.             /* expand groups */
  22569.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22570.     and     p.protecttype <> 206    /* only grant rows */
  22571.     and     p.action in (26,193,195,196,197)
  22572.     and     o.uid <> u.uid            /* no rows for owner */
  22573.     and     not exists (            /* exclude revoke'd privileges */
  22574.             select     *
  22575.             from     sysprotects p1
  22576.             where    p1.protecttype = 206
  22577.             and     p1.action = p.action
  22578.             and     p1.id = p.id
  22579.             and     p1.uid = u.uid)
  22580.     union
  22581.     select    /*    Add rows for table owner */
  22582.         GRANTOR        = user_name(u.uid),
  22583.         GRANTEE        = user_name(o.uid),
  22584.         TABLE_CATALOG    = db_name(),
  22585.         TABLE_SCHEMA    = user_name(o.uid),
  22586.         TABLE_NAME    = o.name,
  22587.         PRIVILEGE_TYPE    = convert(varchar(30),
  22588.                     case v.number
  22589.                     when 193 then 'SELECT'
  22590.                     when 195 then 'INSERT'
  22591.                     when 196 then 'DELETE'
  22592.                     when 197 then 'UPDATE'
  22593.                     else 'REFERENCES'
  22594.                     end),
  22595.         IS_GRANTABLE    = convert(bit,1)    
  22596.     from 
  22597.         sysobjects o, master.dbo.spt_values v, sysusers u
  22598.     where
  22599.         o.type in ('U','V','S')
  22600.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22601.     and    (@grantee is null or @grantee = user_name(o.uid))
  22602.     and     u.uid = 1        /* grantor is dbo of database */
  22603.     and    (@grantor is null or @grantor = user_name(u.uid))
  22604.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22605.     and     v.number in (26,193,195,196,197)
  22606.     and     not exists (    /* exclude revoke'd privileges */
  22607.             select     *
  22608.             from     sysprotects p1
  22609.             where    p1.protecttype = 206
  22610.             and     p1.action = v.number
  22611.             and     p1.id = o.id
  22612.             and     p1.uid = o.uid)
  22613.     order by 4,5,6,1,2
  22614.     END
  22615.  
  22616. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  22617.     @scrollopt output, @ccopt output, @rows output
  22618.  
  22619. drop table #sptprivsrowset1
  22620. return isnull(@ret,0)
  22621. go
  22622. dump tran master with no_log
  22623. go
  22624. CREATE PROCEDURE sp_table_privileges_rowset;3
  22625. as
  22626.     select
  22627.         GRANTOR        = convert(sysname, ' '),
  22628.         GRANTEE        = convert(sysname, ' '),
  22629.         TABLE_CATALOG    = convert(sysname, ' '),
  22630.         TABLE_SCHEMA    = convert(sysname, ' '),
  22631.         TABLE_NAME    = convert(sysname, ' '),
  22632.         PRIVILEGE_TYPE    = convert(varchar(30), ' '),
  22633.         IS_GRANTABLE    = convert(bit, 0)
  22634.     where    1=0
  22635. go
  22636.  
  22637. if (charindex('7.00', @@version) = 0 and
  22638.     charindex('8.00', @@version) = 0)
  22639. begin
  22640.     print ''
  22641.     print ''
  22642.     print 'Warning:'
  22643.     print 'you are installing the stored procedures '
  22644.     print 'on a pre 8.0 SQL Server.'
  22645.     print 'Ignore the following errors.'
  22646. end
  22647. else
  22648.     drop proc sp_table_privileges_rowset
  22649. go
  22650.  
  22651.  
  22652. /*    Procedure for 8.0 server */
  22653. CREATE PROCEDURE sp_table_privileges_rowset
  22654.     (
  22655.     @table_name         sysname,
  22656.     @table_schema        sysname = null,
  22657.     @grantor        sysname = null,
  22658.     @grantee        sysname = null
  22659.     )
  22660. as
  22661.     select
  22662.         GRANTOR        = user_name(p.grantor),
  22663.         GRANTEE        = user_name(u.uid),
  22664.         TABLE_CATALOG    = db_name(),
  22665.         TABLE_SCHEMA    = user_name(o.uid),
  22666.         TABLE_NAME    = o.name,
  22667.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  22668.                     case p.action
  22669.                     when 193 then N'SELECT'
  22670.                     when 195 then N'INSERT'
  22671.                     when 196 then N'DELETE'
  22672.                     when 197 then N'UPDATE'
  22673.                     else N'REFERENCES'
  22674.                     end),
  22675.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22676.     from 
  22677.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  22678.     where
  22679.         o.name = @table_name
  22680.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22681.     and    (@grantee is null or @grantee = user_name(u.uid))
  22682.     and     o.type in ('U','V','S')
  22683.     and    p.id = o.id
  22684.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22685.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  22686.     and     (u.uid > 0 and u.uid < 16384)
  22687.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  22688.     and     p.protecttype <> 206    /* only grant rows */
  22689.     and     p.action in (26,193,195,196,197)
  22690.     and     o.uid <> u.uid            /* no rows for owner */
  22691.     and     not exists (            /* exclude revoke'd privileges */
  22692.             select     *
  22693.             from     sysprotects p1
  22694.             where    p1.protecttype = 206
  22695.             and     p1.action = p.action
  22696.             and     p1.id = p.id
  22697.             and     p1.uid = u.uid)
  22698.     union
  22699.     select    /*    Add rows for table owner */
  22700.         GRANTOR        = user_name(u.uid),
  22701.         GRANTEE        = user_name(o.uid),
  22702.         TABLE_CATALOG    = db_name(),
  22703.         TABLE_SCHEMA    = user_name(o.uid),
  22704.         TABLE_NAME    = o.name,
  22705.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  22706.                     case v.number
  22707.                     when 193 then N'SELECT'
  22708.                     when 195 then N'INSERT'
  22709.                     when 196 then N'DELETE'
  22710.                     when 197 then N'UPDATE'
  22711.                     else N'REFERENCES'
  22712.                     end),
  22713.         IS_GRANTABLE    = convert(bit,1)    
  22714.     from 
  22715.         sysobjects o, master.dbo.spt_values v, sysusers u
  22716.     where
  22717.         o.name = @table_name
  22718.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22719.     and    (@grantee is null or @grantee = user_name(o.uid))
  22720.     and     o.type in ('U','V','S')
  22721.     and     u.uid = 1        /* grantor is dbo of database */
  22722.     and    (@grantor is null or @grantor = user_name(u.uid))
  22723.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22724.     and     v.number in (26,193,195,196,197)
  22725.     and     not exists (    /* exclude revoke'd privileges */
  22726.             select     *
  22727.             from     sysprotects p1
  22728.             where    p1.protecttype = 206
  22729.             and     p1.action = v.number
  22730.             and     p1.id = o.id
  22731.             and     p1.uid = o.uid)
  22732.     order by 4,5,6,1,2
  22733. go
  22734. dump tran master with no_log
  22735. go
  22736. CREATE PROCEDURE sp_table_privileges_rowset;2
  22737.     (
  22738.     @table_schema        sysname = null,
  22739.     @grantor        sysname = null,
  22740.     @grantee        sysname = null
  22741.     )
  22742. as
  22743.     select
  22744.         GRANTOR        = user_name(p.grantor),
  22745.         GRANTEE        = user_name(u.uid),
  22746.         TABLE_CATALOG    = db_name(),
  22747.         TABLE_SCHEMA    = user_name(o.uid),
  22748.         TABLE_NAME    = o.name,
  22749.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  22750.                     case p.action
  22751.                     when 193 then N'SELECT'
  22752.                     when 195 then N'INSERT'
  22753.                     when 196 then N'DELETE'
  22754.                     when 197 then N'UPDATE'
  22755.                     else N'REFERENCES'
  22756.                     end),
  22757.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22758.     from 
  22759.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  22760.     where
  22761.         o.type in ('U','V','S')
  22762.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22763.     and    (@grantee is null or @grantee = user_name(u.uid))
  22764.     and    p.id = o.id
  22765.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22766.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  22767.     and     (u.uid > 0 and u.uid < 16384)
  22768.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  22769.     and     p.protecttype <> 206    /* only grant rows */
  22770.     and     p.action in (26,193,195,196,197)
  22771.     and     o.uid <> u.uid            /* no rows for owner */
  22772.     and     not exists (            /* exclude revoke'd privileges */
  22773.             select     *
  22774.             from     sysprotects p1
  22775.             where    p1.protecttype = 206
  22776.             and     p1.action = p.action
  22777.             and     p1.id = p.id
  22778.             and     p1.uid = u.uid)
  22779.     union
  22780.     select    /*    Add rows for table owner */
  22781.         GRANTOR        = user_name(u.uid),
  22782.         GRANTEE        = user_name(o.uid),
  22783.         TABLE_CATALOG    = db_name(),
  22784.         TABLE_SCHEMA    = user_name(o.uid),
  22785.         TABLE_NAME    = o.name,
  22786.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  22787.                     case v.number
  22788.                     when 193 then N'SELECT'
  22789.                     when 195 then N'INSERT'
  22790.                     when 196 then N'DELETE'
  22791.                     when 197 then N'UPDATE'
  22792.                     else N'REFERENCES'
  22793.                     end),
  22794.         IS_GRANTABLE    = convert(bit,1)    
  22795.     from 
  22796.         sysobjects o, master.dbo.spt_values v, sysusers u
  22797.     where
  22798.         o.type in ('U','V','S')
  22799.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22800.     and    (@grantee is null or @grantee = user_name(o.uid))
  22801.     and     u.uid = 1        /* grantor is dbo of database */
  22802.     and    (@grantor is null or @grantor = user_name(u.uid))
  22803.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22804.     and     v.number in (26,193,195,196,197)
  22805.     and     not exists (    /* exclude revoke'd privileges */
  22806.             select     *
  22807.             from     sysprotects p1
  22808.             where    p1.protecttype = 206
  22809.             and     p1.action = v.number
  22810.             and     p1.id = o.id
  22811.             and     p1.uid = o.uid)
  22812.     order by 4,5,6,1,2
  22813. go
  22814. dump tran master with no_log
  22815. go
  22816. create procedure sp_table_privileges_rowset;5
  22817.     (
  22818.     @table_server        sysname,
  22819.     @table_catalog        sysname = null,
  22820.     @table_name        sysname = null,
  22821.     @table_schema        sysname = null,
  22822.     @grantor        sysname = null,
  22823.     @grantee        sysname = null
  22824.     )
  22825. as
  22826.     select
  22827.         GRANTOR,        
  22828.         GRANTEE,        
  22829.         TABLE_CATALOG,    
  22830.         TABLE_SCHEMA,    
  22831.         TABLE_NAME,    
  22832.         PRIVILEGE_TYPE,    
  22833.         IS_GRANTABLE    
  22834.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  22835.                 @table_server,
  22836.                 @table_catalog,
  22837.                 @table_schema,
  22838.                 @table_name,
  22839.                 @grantor,
  22840.                 @grantee >
  22841.     order by 3,4,5,6,1,2
  22842. go
  22843.  
  22844. grant execute on sp_table_privileges_rowset to public
  22845. go
  22846. dump tran master with no_log
  22847. go
  22848. if (charindex('6.00', @@version) > 0)
  22849.     begin
  22850.     if (exists (select * from sysobjects
  22851.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  22852.         begin
  22853.         drop proc sp_table_privileges_rowset
  22854.         dump tran master with no_log
  22855.         end
  22856.     end
  22857. go
  22858.  
  22859.  
  22860. print ''
  22861. print 'creating sp_linkedservers_rowset'
  22862. go
  22863. if (charindex('7.00', @@version) = 0 and
  22864.     charindex('8.00', @@version) = 0)
  22865. begin
  22866.     print ''
  22867.     print ''
  22868.     print 'Warning:'
  22869.     print 'you are installing the stored procedures '
  22870.     print 'on a pre 8.0 SQL Server.'
  22871.     print 'Ignore the following errors.'
  22872. end
  22873. go
  22874. /*    Procedure for 8.0 server */
  22875. create proc sp_linkedservers_rowset
  22876.     (
  22877.     @srvname     sysname
  22878.     )
  22879. as
  22880.     select
  22881.         SVR_NAME         = srvname,
  22882.         SVR_PRODUCT        = srvproduct,
  22883.         SVR_PROVIDERNAME    = providername,
  22884.         SVR_DATASOURCE        = datasource,
  22885.         SVR_PROVIDERSTRING    = providerstring,
  22886.         SVR_LOCATION        = location,
  22887.         SVR_CATALOG        = catalog
  22888.     from master.dbo.sysservers
  22889.     where srvname = @srvname and (srvstatus & 128) = 128
  22890.     order by 1
  22891. go
  22892. dump tran master with no_log
  22893. go
  22894. create proc sp_linkedservers_rowset;2
  22895. as
  22896.     select
  22897.         SVR_NAME         = srvname,
  22898.         SVR_PRODUCT        = srvproduct,
  22899.         SVR_PROVIDERNAME    = providername,
  22900.         SVR_DATASOURCE        = datasource,
  22901.         SVR_PROVIDERSTRING    = providerstring,
  22902.         SVR_LOCATION        = location,
  22903.         SVR_CATALOG        = catalog
  22904.     from master.dbo.sysservers
  22905.     where (srvstatus & 128) = 128
  22906.     order by 1
  22907. go
  22908.  
  22909. grant execute on sp_linkedservers_rowset to public
  22910. go
  22911.  
  22912. dump tran master with no_log
  22913. go
  22914.  
  22915. print ''
  22916. print 'creating sp_table_statistics_rowset'
  22917. go
  22918. if (charindex('8.00', @@version) = 0)
  22919. begin
  22920.     print ''
  22921.     print ''
  22922.     print 'Warning:'
  22923.     print 'you are installing the stored procedures '
  22924.     print 'on a pre 8.00 SQL Server.'
  22925.     print 'Ignore the following errors.'
  22926. end
  22927. go
  22928. /*    Procedure for 8.00 server */
  22929. create proc sp_table_statistics_rowset
  22930. as
  22931.     select
  22932.         TABLE_CATALOG = convert(sysname, null),
  22933.         TABLE_SCHEMA = convert(sysname, null),
  22934.         TABLE_NAME = convert(sysname, null),
  22935.         STATISTICS_CATALOG = convert(sysname, null),
  22936.         STATISTICS_SCHEMA = convert(sysname, null),
  22937.         STATISTICS_NAME = convert(sysname, null),
  22938.         STATISTICS_TYPE = convert(smallint,0),
  22939.         COLUMN_NAME = convert(sysname, null),
  22940.         COLUMN_GUID = convert(uniqueidentifier, null),
  22941.         COLUMN_PROPID = convert(int, null),
  22942.         ORDINAL_POSITION = convert(int, null),
  22943.         SAMPLE_PCT = convert(smallint, null),
  22944.         LAST_UPDATE_TIME = convert(datetime, null),
  22945.         NO_OF_RANGES = convert(int, null),
  22946.         COLUMN_CARDINALITY = convert(bigint, null),
  22947.         TUPLE_CARDINALITY = convert(bigint, null),
  22948.         TABLE_CARDINALITY = convert(bigint, null),
  22949.         AVG_COLUMN_LENGTH = convert(int, null)
  22950.     where 1=0
  22951. go
  22952. dump tran master with no_log
  22953. go
  22954.  
  22955. create proc sp_table_statistics_rowset;2
  22956.                 (@table_catalog        sysname = null,
  22957.                 @table_schema        sysname = null,
  22958.                 @table_name        sysname = null,
  22959.                 @stat_catalog        sysname = null,
  22960.                 @stat_schema        sysname = null,
  22961.                 @stat_name        sysname = null)
  22962. as 
  22963. begin
  22964.     
  22965.     set nocount on
  22966.  
  22967.     -- check in parameters
  22968.     if ((@table_catalog is not null) and (db_name() <> @table_catalog)
  22969.         or (@stat_catalog is not null) and (db_name() <> @stat_catalog))
  22970.         begin    /* If qualifier doesn't match current database */
  22971.             raiserror 20001 '~~Rush_5~~'
  22972.             return
  22973.         end
  22974.  
  22975.     --- create temp table
  22976.     create table #spstattab(
  22977.         tblcatalog    sysname collate database_default not null,
  22978.         tblschema    sysname collate database_default not null,
  22979.         tblname        sysname collate database_default not null,
  22980.         statname    sysname collate database_default not null
  22981.         )
  22982.  
  22983.     insert into #spstattab
  22984.     select 
  22985.         db_name(),
  22986.         user_name(o.uid),
  22987.         o.name,
  22988.         x.name
  22989.     from sysobjects o, sysindexes x
  22990.     where o.type in ('U')
  22991.         and (@table_name is null or o.name = @table_name)
  22992.         and (@table_schema is null or @table_schema = user_name(o.uid))
  22993.         and (@stat_name is null or @stat_name = x.name)
  22994.         and (@stat_schema is null or @stat_schema = user_name(o.uid))
  22995.         and x.id = o.id
  22996.         and permissions(o.id) <> 0
  22997.         and x.statblob is not null
  22998.     order by 1,2,3,4
  22999.  
  23000.     -- fast forward CURSOR OVER THE temp TABLE
  23001.     declare ff_csr  cursor fast_forward for
  23002.     select tblcatalog, tblschema, tblname, statname from #spstattab
  23003.  
  23004.     declare @tblcatalog    sysname,
  23005.         @tblschema    sysname,
  23006.         @tblname    sysname,
  23007.         @statname    sysname,
  23008.         @qtbl        nvarchar(4000),
  23009.         @rowsetcount    int
  23010.  
  23011.     set @rowsetcount = 0
  23012.  
  23013.     open ff_csr
  23014.     fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23015.     while @@fetch_status >= 0
  23016.     begin
  23017.         set @rowsetcount = @rowsetcount + 1
  23018.         set @qtbl = @tblcatalog + '.' + @tblschema + '.' + @tblname
  23019.         dbcc show_statistics(@qtbl, @statname) with stat_header join density_vector
  23020.         fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23021.     end
  23022.     close ff_csr
  23023.     deallocate ff_csr
  23024.     
  23025.     --- drop temp table
  23026.     drop table #spstattab
  23027.  
  23028.     if @rowsetcount = 0
  23029.     begin
  23030.         select    
  23031.             TABLE_CATALOG = convert(sysname, null),
  23032.             TABLE_SCHEMA = convert(sysname, null),
  23033.             TABLE_NAME = convert(sysname, null),
  23034.             STATISTICS_CATALOG = convert(sysname, null),
  23035.             STATISTICS_SCHEMA = convert(sysname, null),
  23036.             STATISTICS_NAME = convert(sysname, null),
  23037.             STATISTICS_TYPE = convert(smallint,0),
  23038.             COLUMN_NAME = convert(sysname, null),
  23039.             COLUMN_GUID = convert(uniqueidentifier, null),
  23040.             COLUMN_PROPID = convert(int, null),
  23041.             ORDINAL_POSITION = convert(int, null),
  23042.             SAMPLE_PCT = convert(smallint, null),
  23043.             LAST_UPDATE_TIME = convert(datetime, null),
  23044.             NO_OF_RANGES = convert(int, null),
  23045.             COLUMN_CARDINALITY = convert(bigint, null),
  23046.             TUPLE_CARDINALITY = convert(bigint, null),
  23047.             TABLE_CARDINALITY = convert(bigint, null),
  23048.             AVG_COLUMN_LENGTH = convert(int, null)
  23049.         where 1=0
  23050.     end
  23051.  
  23052.     set nocount off
  23053. end
  23054. go
  23055.  
  23056. grant execute on sp_table_statistics_rowset to public
  23057. go
  23058.  
  23059. dump tran master with no_log
  23060. go
  23061.  
  23062. print ''
  23063. print 'creating sp_oledb_column_constraints'
  23064. go
  23065.  
  23066. /* Procedure for 6.5 server */
  23067. create procedure sp_oledb_column_constraints
  23068.     (@tblname varchar(225)        -- the table to check for constraints
  23069.     ,@colname varchar(225)        -- the column to check for constraints
  23070.     ,@dropcnst smallint)
  23071. as
  23072. begin
  23073.     -- PRELIM
  23074.     set nocount on
  23075.  
  23076.     declare    @tblid        int           -- the object id of the table
  23077.         ,@cnstname        varchar(225)   -- name of const. currently under consideration
  23078.         ,@cnstid        int
  23079.         ,@cnstatus        int
  23080.         ,@dbname        varchar(30)
  23081.  
  23082.     -- Create temp table
  23083.     create table #spcnsttab
  23084.     (cnst_name        varchar(225)        NOT NULL)
  23085.  
  23086.     -- Check to see that the object names are local to the current database.
  23087.     if @tblname like '%.%.%'
  23088.           and  substring(@tblname, 1, charindex('.', @tblname) - 1) <> db_name()
  23089.        begin
  23090.            raiserror(15250,-1,-1)
  23091.            return (1)
  23092.        end
  23093.     -- Check to see if the table exists and initialize @objid.
  23094.     select @tblid = object_id(@tblname)
  23095.     if @tblid is NULL
  23096.     begin
  23097.         select @dbname=db_name()
  23098.         raiserror(15009,-1,-1,@tblname,@dbname)
  23099.         return (1)
  23100.     end
  23101.  
  23102.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23103.     declare cnst_csr insensitive cursor for
  23104.         select c.constid, c.status, o.name 
  23105.         from sysconstraints c, sysobjects o 
  23106.         where c.id = @tblid and o.id = c.constid
  23107.             and ((c.status & 0xf)=1 and (@dropcnst & 16)<> 0
  23108.             or (c.status & 0xf)=2 and (@dropcnst & 32)<> 0 
  23109.             or (c.status & 0xf)=5 and (@dropcnst & 2)<> 0)    
  23110.         -- ONLY 6.5 sysconstraints objects
  23111.         for read only
  23112.  
  23113.     -- Now check out each constraint, figure out its type and keys and
  23114.     -- save the info in a temporary table that we'll print out at the end.
  23115.     open cnst_csr
  23116.     fetch cnst_csr into @cnstid, @cnstatus, @cnstname
  23117.     while @@fetch_status >= 0
  23118.     begin
  23119.         if ((@cnstatus & 0xf) in (1, 2)) ---- primary key, unique
  23120.             begin
  23121.                 -- get indid
  23122.                 declare @indid smallint
  23123.                 select    @indid = indid
  23124.                 from    sysindexes
  23125.                 where    name = object_name(@cnstid) and id = @tblid
  23126.  
  23127.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23128.                     and (index_col(@tblname, @indid, 2) is null)
  23129.                     begin
  23130.                         -- ADD TO TABLE
  23131.                         insert into #spcnsttab
  23132.                         values (@cnstname)
  23133.                     end
  23134.             end
  23135.         else
  23136.         if (@cnstatus & 0xf) = 5
  23137.             begin
  23138.                 if (select col_name(id, colid) from sysconstraints where colid > 0 and constid=@cnstid) = @colname
  23139.                     begin
  23140.                         insert into #spcnsttab
  23141.                         values (@cnstname)
  23142.                     end
  23143.             end
  23144.  
  23145.         fetch cnst_csr into @cnstid ,@cnstatus ,@cnstname
  23146.     end        --of major loop
  23147.     close cnst_csr
  23148.     deallocate cnst_csr
  23149.  
  23150.     -- Now print out the contents of the temporary index table.
  23151.     select 'constraint_name' = cnst_name
  23152.     from #spcnsttab 
  23153.     
  23154.     drop table #spcnsttab
  23155.  
  23156.     set nocount off
  23157.     return (0)
  23158. end    
  23159. go
  23160. dump tran master with no_log
  23161. go
  23162.  
  23163. if (charindex('7.00', @@version) = 0 and
  23164.     charindex('8.00', @@version) = 0)
  23165. begin
  23166.     print ''
  23167.     print ''
  23168.     print 'Warning:'
  23169.     print 'you are installing the stored procedures '
  23170.     print 'on a pre 7.0 SQL Server.'
  23171.     print 'Ignore the following errors.'
  23172. end
  23173. else
  23174.     drop proc sp_oledb_column_constraints
  23175. go
  23176.  
  23177. /* Procedure for 7.0 server */
  23178. create procedure sp_oledb_column_constraints
  23179.     (@tblname nvarchar(776)        -- the table to check for constraints
  23180.     ,@colname nvarchar(776)        -- the column to check for constraints
  23181.     ,@dropcnst smallint)
  23182. as
  23183. begin
  23184.     -- PRELIM
  23185.     set nocount on
  23186.  
  23187.     declare    @tblid        int           -- the object id of the table
  23188.         ,@cnstname        sysname       -- name of const. currently under consideration
  23189.         ,@cnstid        int
  23190.         ,@cnsttype        character(2)
  23191.         ,@dbname        sysname
  23192.  
  23193.     -- Create temp table
  23194.     create table #spcnsttab
  23195.     (cnst_name        sysname        NOT NULL)
  23196.  
  23197.     -- Check to see that the object names are local to the current database.
  23198.     select @dbname = parsename(@tblname,3) 
  23199.     if @dbname is not null and @dbname <> db_name()
  23200.     begin
  23201.         raiserror(15250,-1,-1)
  23202.         return (1)
  23203.     end
  23204.  
  23205.     -- Check to see if the table exists and initialize @objid.
  23206.     select @tblid = object_id(@tblname)
  23207.     if @tblid is NULL
  23208.     begin
  23209.         select @dbname=db_name()
  23210.         raiserror(15009,-1,-1,@tblname,@dbname)
  23211.         return (1)
  23212.     end
  23213.  
  23214.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23215.     declare cnst_csr cursor fast_forward for
  23216.         select id, xtype, name 
  23217.         from sysobjects 
  23218.         where parent_obj = @tblid
  23219.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  23220.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  23221.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))    
  23222.         -- ONLY 6.5 sysconstraints objects
  23223.         for read only
  23224.  
  23225.     -- Now check out each constraint, figure out its type and keys and
  23226.     -- save the info in a temporary table that we'll print out at the end.
  23227.     open cnst_csr
  23228.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  23229.     while @@fetch_status >= 0
  23230.     begin
  23231.         if @cnsttype in ('PK','UQ')
  23232.             begin
  23233.                 -- get indid
  23234.                 declare @indid smallint
  23235.                 select    @indid = indid
  23236.                 from    sysindexes
  23237.                 where    name = object_name(@cnstid) and id = @tblid
  23238.  
  23239.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23240.                     and (index_col(@tblname, @indid, 2) is null)
  23241.                     begin
  23242.                         -- ADD TO TABLE
  23243.                         insert into #spcnsttab
  23244.                         values (@cnstname)
  23245.                     end
  23246.             end
  23247.         else
  23248.         if (@cnsttype = 'D ')
  23249.             begin
  23250.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  23251.                     begin
  23252.                         insert into #spcnsttab
  23253.                         values (@cnstname)
  23254.                     end
  23255.             end
  23256.  
  23257.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  23258.     end        --of major loop
  23259.     close cnst_csr
  23260.     deallocate cnst_csr
  23261.  
  23262.     -- Now print out the contents of the temporary index table.
  23263.     select 'constraint_name' = cnst_name
  23264.     from #spcnsttab 
  23265.  
  23266.     drop table #spcnsttab
  23267.     
  23268.     set nocount off
  23269.     return (0)
  23270. end
  23271. go
  23272.  
  23273. if (charindex('8.00', @@version) = 0)
  23274. begin
  23275.     print ''
  23276.     print ''
  23277.     print 'Warning:'
  23278.     print 'you are installing the stored procedures '
  23279.     print 'on a pre 8.0 SQL Server.'
  23280.     print 'Ignore the following errors.'
  23281. end
  23282. else
  23283.     drop proc sp_oledb_column_constraints
  23284. go
  23285.  
  23286. /* Procedure for 8.0 server */
  23287. create procedure sp_oledb_column_constraints
  23288.     (@tblname nvarchar(776)        -- the table to check for constraints
  23289.     ,@colname nvarchar(776)        -- the column to check for constraints
  23290.     ,@dropcnst smallint)
  23291. as
  23292. begin
  23293.     -- PRELIM
  23294.     set nocount on
  23295.  
  23296.     declare    @tblid        int           -- the object id of the table
  23297.         ,@cnstname        sysname       -- name of const. currently under consideration
  23298.         ,@cnstid        int
  23299.         ,@cnsttype        character(2)
  23300.         ,@dbname        sysname
  23301.  
  23302.     -- Create temp table
  23303.     create table #spcnsttab
  23304.     (cnst_name        sysname        collate database_default NOT NULL)
  23305.  
  23306.     -- Check to see that the object names are local to the current database.
  23307.     select @dbname = parsename(@tblname,3) 
  23308.     if @dbname is not null and @dbname <> db_name()
  23309.     begin
  23310.         raiserror(15250,-1,-1)
  23311.         return (1)
  23312.     end
  23313.  
  23314.     -- Check to see if the table exists and initialize @objid.
  23315.     select @tblid = object_id(@tblname)
  23316.     if @tblid is NULL
  23317.     begin
  23318.         select @dbname=db_name()
  23319.         raiserror(15009,-1,-1,@tblname,@dbname)
  23320.         return (1)
  23321.     end
  23322.  
  23323.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23324.     declare cnst_csr cursor fast_forward for
  23325.         select id, xtype, name 
  23326.         from sysobjects 
  23327.         where parent_obj = @tblid
  23328.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  23329.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  23330.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))    
  23331.         -- ONLY 6.5 sysconstraints objects
  23332.         for read only
  23333.  
  23334.     -- Now check out each constraint, figure out its type and keys and
  23335.     -- save the info in a temporary table that we'll print out at the end.
  23336.     open cnst_csr
  23337.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  23338.     while @@fetch_status >= 0
  23339.     begin
  23340.         if @cnsttype in ('PK','UQ')
  23341.             begin
  23342.                 -- get indid
  23343.                 declare @indid smallint
  23344.                 select    @indid = indid
  23345.                 from    sysindexes
  23346.                 where    name = object_name(@cnstid) and id = @tblid
  23347.  
  23348.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23349.                     and (index_col(@tblname, @indid, 2) is null)
  23350.                     begin
  23351.                         -- ADD TO TABLE
  23352.                         insert into #spcnsttab
  23353.                         values (@cnstname)
  23354.                     end
  23355.             end
  23356.         else
  23357.         if (@cnsttype = 'D ')
  23358.             begin
  23359.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  23360.                     begin
  23361.                         insert into #spcnsttab
  23362.                         values (@cnstname)
  23363.                     end
  23364.             end
  23365.  
  23366.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  23367.     end        --of major loop
  23368.     close cnst_csr
  23369.     deallocate cnst_csr
  23370.  
  23371.     -- Now print out the contents of the temporary index table.
  23372.     select 'constraint_name' = cnst_name
  23373.     from #spcnsttab 
  23374.  
  23375.     drop table #spcnsttab
  23376.     
  23377.     set nocount off
  23378.     return (0)
  23379. end
  23380. go
  23381.  
  23382. grant execute on sp_oledb_column_constraints to public
  23383. go
  23384.  
  23385. dump tran master with no_log
  23386. go
  23387.  
  23388. print ''
  23389. print 'creating sp_oledb_indexinfo'
  23390. go
  23391.  
  23392. /* Procedure for 6.5 server */
  23393. create procedure sp_oledb_indexinfo
  23394.         @objname varchar(225)        -- the table to check for indexes
  23395.         ,@indname varchar(225)
  23396. as
  23397. begin
  23398.     -- PRELIM
  23399.     set nocount on
  23400.  
  23401.     declare @objid int,            -- the object id of the table
  23402.         @indid smallint,    -- the index id of an index
  23403.         @status int,
  23404.         @dbname    varchar(30),
  23405.         @OrigFillFactor tinyint,
  23406.         @i int, 
  23407.         @thiskey varchar(32),
  23408.         @tptr     varbinary(16) -- pointer for building text strings.
  23409.     ---- Check to see that the object names are local to the current database.
  23410.     if @objname like '%.%.%'
  23411.           and  substring(@objname, 1, charindex('.', @objname) - 1) <> db_name()
  23412.        begin
  23413.            raiserror(15250,-1,-1)
  23414.            return (1)
  23415.        end
  23416.     ---- Check to see if the table exists and initialize @objid.
  23417.     select @objid = object_id(@objname)
  23418.     ---- Table does not exist so return.
  23419.     if @objid is NULL
  23420.        begin
  23421.            select @dbname=db_name()
  23422.            raiserror(15009,-1,-1,@objname,@dbname)
  23423.            return (1)
  23424.        end
  23425.  
  23426.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  23427.     from sysindexes
  23428.     where id = @objid and name = @indname 
  23429.  
  23430.     -- IF NO INDEX, QUIT
  23431.     if @indid is NULL
  23432.     begin
  23433.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  23434.         return (1)
  23435.     end
  23436.     -- create temp table
  23437.     create table #spindtab
  23438.     (
  23439.         status                int,
  23440.         OrigFillFactor            tinyint,
  23441.         index_keys            text    NOT NULL,
  23442.     )
  23443.     -- Now check out each index, figure out its type and keys and
  23444.     -- First we'll figure out what the keys are.
  23445.     select @i = 1
  23446.     while (@i <= 16)
  23447.         begin
  23448.             select @thiskey = index_col(@objname, @indid, @i)
  23449.              if @thiskey is NULL
  23450.                     goto keysdone
  23451.              if @i=1
  23452.                 begin
  23453.                     insert into #spindtab
  23454.                     values (@status, @OrigFillFactor, @thiskey)
  23455.  
  23456.             select @tptr = textptr(index_keys) from #spindtab
  23457.                 end
  23458.              else
  23459.                 begin
  23460.                     select @thiskey = ', ' + @thiskey
  23461.  
  23462.             if @tptr is not null
  23463.                        updatetext #spindtab.index_keys @tptr null null @thiskey
  23464.                 end
  23465.  
  23466.         select @i = @i + 1
  23467.          end --loop 16
  23468.          ---- When we get here we now have all the keys.
  23469.  
  23470. keysdone:
  23471.     select * from #spindtab
  23472.     
  23473.     drop table #spindtab
  23474.     set nocount off
  23475.     return (0)
  23476. end
  23477. go
  23478. dump tran master with no_log
  23479. go
  23480.  
  23481. if (charindex('7.00', @@version) = 0 and
  23482.     charindex('8.00', @@version) = 0)
  23483. begin
  23484.     print ''
  23485.     print ''
  23486.     print 'Warning:'
  23487.     print 'you are installing the stored procedures '
  23488.     print 'on a pre 8.0 SQL Server.'
  23489.     print 'Ignore the following errors.'
  23490. end
  23491. else
  23492.     drop proc sp_oledb_indexinfo
  23493. go
  23494.  
  23495. /* Procedure for 8.0 server */
  23496. create proc sp_oledb_indexinfo
  23497.         @objname nvarchar(776)        -- the table to check for indexes
  23498.         ,@indname nvarchar(776)
  23499. as
  23500. begin
  23501.     -- PRELIM
  23502.     set nocount on
  23503.  
  23504.     declare @objid int,            -- the object id of the table
  23505.         @indid smallint,    -- the index id of an index
  23506.         @status int,
  23507.         @keys nvarchar(2078),-- string build index key list, length = (16*max_id_length)+(15*2)
  23508.         @dbname    sysname,
  23509.         @OrigFillFactor tinyint
  23510.  
  23511.     -- Check to see that the object names are local to the current database.
  23512.     select @dbname = parsename(@objname,3) 
  23513.  
  23514.     if @dbname is not null and @dbname <> db_name()
  23515.     begin
  23516.             raiserror(15250,-1,-1)
  23517.             return (1)
  23518.     end
  23519.  
  23520.     -- Check to see the the table exists and initialize @objid.
  23521.     select @objid = object_id(@objname)
  23522.     if @objid is NULL
  23523.     begin
  23524.         select @dbname=db_name()
  23525.         raiserror(15009,-1,-1,@objname,@dbname)
  23526.         return (1)
  23527.     end
  23528.  
  23529.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  23530.     from sysindexes
  23531.     where id = @objid and name = @indname 
  23532.  
  23533.     -- IF NO INDEX, QUIT
  23534.     if @indid is NULL
  23535.     begin
  23536.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  23537.         return (1)
  23538.     end
  23539.  
  23540.     -- First we'll figure out what the keys are.
  23541.     declare @i int, @thiskey sysname
  23542.     select @keys = index_col(@objname, @indid, 1),
  23543.         @i = 2, @thiskey = index_col(@objname, @indid, 2)
  23544.     while (@thiskey is not null )
  23545.     begin
  23546.         select @keys = @keys + ', ' + @thiskey, @i = @i + 1
  23547.         select @thiskey = index_col(@objname, @indid, @i)
  23548.     end
  23549.     -- DISPLAY THE RESULTS
  23550.     select 'Status'=@status, 'OrigFillFactor' =@OrigFillFactor, 'Index keys'=@keys
  23551.     set nocount off
  23552.     return (0) 
  23553. end
  23554. go
  23555.  
  23556. grant execute on sp_oledb_indexinfo to public
  23557. go
  23558.  
  23559. dump tran master with no_log
  23560. go
  23561.  
  23562. print ''
  23563. print 'creating sp_oledb_ro_usrname'
  23564. go
  23565.  
  23566. create procedure sp_oledb_ro_usrname
  23567. as
  23568. begin
  23569.     select substring('NY',status/1024&1+1,1),user_name() 
  23570.     from master..sysdatabases 
  23571.     where name=DB_NAME()
  23572. end
  23573. go
  23574.  
  23575. grant execute on sp_oledb_ro_usrname to public
  23576. go
  23577.  
  23578. dump tran master with no_log
  23579. go
  23580.  
  23581. if (charindex('6.00', @@version) > 0 or
  23582.     charindex('6.50', @@version) > 0 or
  23583.     charindex('7.00', @@version) > 0)
  23584.     begin
  23585.     if (exists (select * from sysobjects
  23586.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  23587.         begin
  23588.             drop proc sp_oledb_ro_usrname
  23589.             dump tran master with no_log
  23590.         end
  23591.     end
  23592. go
  23593.  
  23594. print ''
  23595. print 'creating sp_oledb_deflang'
  23596. go
  23597.  
  23598. if (charindex('8.00', @@version) = 0)
  23599. begin
  23600.     print ''
  23601.     print ''
  23602.     print 'Warning:'
  23603.     print 'you are installing the stored procedures '
  23604.     print 'on a pre 8.0 SQL Server.'
  23605.     print 'Ignore the following errors.'
  23606. end
  23607. go
  23608.  
  23609. create procedure sp_oledb_deflang
  23610. as
  23611.     begin
  23612.         select ISNULL(language,'us_english') 
  23613.         from master..syslogins 
  23614.         where sid=SUSER_SID()
  23615.     end
  23616. go
  23617.  
  23618. grant execute on sp_oledb_deflang to public
  23619. go
  23620.  
  23621. dump tran master with no_log
  23622. go
  23623.  
  23624. if (charindex('6.00', @@version) > 0 or
  23625.     charindex('6.50', @@version) > 0 or
  23626.     charindex('7.00', @@version) > 0)
  23627.     begin
  23628.     if (exists (select * from sysobjects
  23629.         where name = 'sp_oledb_deflang' and type = 'P '))
  23630.         begin
  23631.             drop proc sp_oledb_deflang
  23632.             dump tran master with no_log
  23633.         end
  23634.     end
  23635. go
  23636.  
  23637. print ''
  23638. print 'creating sp_oledb_defdb'
  23639. go
  23640.  
  23641. if (charindex('8.00', @@version) = 0)
  23642. begin
  23643.     print ''
  23644.     print ''
  23645.     print 'Warning:'
  23646.     print 'you are installing the stored procedures '
  23647.     print 'on a pre 8.0 SQL Server.'
  23648.     print 'Ignore the following errors.'
  23649. end
  23650. go
  23651.  
  23652. create procedure sp_oledb_defdb
  23653. as
  23654.     begin
  23655.         select dbname from master..syslogins where sid=SUSER_SID()
  23656.     end
  23657. go
  23658.  
  23659. grant execute on sp_oledb_defdb to public
  23660. go
  23661.  
  23662. dump tran master with no_log
  23663. go
  23664.  
  23665. if (charindex('6.00', @@version) > 0 or
  23666.     charindex('6.50', @@version) > 0 or
  23667.     charindex('7.00', @@version) > 0)
  23668.     begin
  23669.     if (exists (select * from sysobjects
  23670.         where name = 'sp_oledb_defdb' and type = 'P '))
  23671.         begin
  23672.             drop proc sp_oledb_defdb
  23673.             dump tran master with no_log
  23674.         end
  23675.     end
  23676. go
  23677.  
  23678. print ''
  23679. print 'creating sp_oledb_database'
  23680. go
  23681.  
  23682. create procedure sp_oledb_database
  23683. as
  23684.     begin
  23685.         select name from master..sysdatabases
  23686.     end
  23687. go
  23688.  
  23689. grant execute on sp_oledb_database to public
  23690. go
  23691.  
  23692. dump tran master with no_log
  23693. go
  23694.  
  23695. if (charindex('6.00', @@version) > 0 or
  23696.     charindex('6.50', @@version) > 0 or
  23697.     charindex('7.00', @@version) > 0)
  23698.     begin
  23699.     if (exists (select * from sysobjects
  23700.         where name = 'sp_oledb_database' and type = 'P '))
  23701.         begin
  23702.             drop proc sp_oledb_database
  23703.             dump tran master with no_log
  23704.         end
  23705.     end
  23706. go
  23707.  
  23708. print ''
  23709. print 'creating sp_oledb_language'
  23710. go
  23711.  
  23712. create procedure sp_oledb_language
  23713. as
  23714. begin
  23715.     select 'English','us_english' 
  23716.     union 
  23717.     select alias,name from master..syslanguages
  23718. end
  23719. go
  23720.  
  23721. grant execute on sp_oledb_language to public
  23722. go
  23723.  
  23724. dump tran master with no_log
  23725. go
  23726. if (charindex('6.00', @@version) > 0 or
  23727.     charindex('6.50', @@version) > 0 or
  23728.     charindex('7.00', @@version) > 0)
  23729.     begin
  23730.     if (exists (select * from sysobjects
  23731.         where name = 'sp_oledb_language' and type = 'P '))
  23732.         begin
  23733.             drop proc sp_oledb_language
  23734.             dump tran master with no_log
  23735.         end
  23736.     end
  23737. go
  23738.  
  23739. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  23740.  
  23741.  
  23742. /*---------------------------- BEGIN BCP CATALOG PROCS ------------------------*/
  23743.  
  23744. print 'creating sp_tablecollations'
  23745. go
  23746.  
  23747. /* Used by BCP to gather all the collation names for a table */
  23748. if (charindex('8.00', @@version) = 0)
  23749. begin
  23750.     print ''
  23751.     print ''
  23752.     print 'Warning:'
  23753.     print 'you are installing the stored procedures '
  23754.     print 'on a pre 8.0 SQL Server.'
  23755.     print 'Ignore the following errors.'
  23756. end
  23757. go
  23758.  
  23759. /* Procedure for 8.0 server */
  23760. create procedure sp_tablecollations (@object nvarchar(4000))
  23761. as
  23762. begin
  23763.     select c.colid,
  23764.            c.name, 
  23765.            tds_collation = c.tdscollation,
  23766.            collation_name = BCPCollationName(c.tdscollation, c.xtype)
  23767.     from syscolumns c inner join sysobjects t on c.id = t.id
  23768.     where t.id = object_id(@object, 'local')
  23769.     order by c.colid
  23770. end
  23771. go
  23772.  
  23773. grant execute on sp_tablecollations to public
  23774. go
  23775.  
  23776. print 'creating sp_bcp_dbcmptlevel'
  23777. go
  23778.  
  23779. /* Used by BCP to get current db compatibility level */
  23780. if (charindex('8.00', @@version) = 0)
  23781. begin
  23782.     print ''
  23783.     print ''
  23784.     print 'Warning:'
  23785.     print 'you are installing the stored procedures '
  23786.     print 'on a pre 8.0 SQL Server.'
  23787.     print 'Ignore the following errors.'
  23788. end
  23789. go
  23790.  
  23791. /* Procedure for 8.0 server */
  23792. create procedure sp_bcp_dbcmptlevel (@dbname sysname)
  23793. as
  23794. begin
  23795.     select cmptlevel
  23796.     from master.dbo.sysdatabases
  23797.     where name = @dbname
  23798. end
  23799. go
  23800.  
  23801. grant execute on sp_bcp_dbcmptlevel to public
  23802. go
  23803.  
  23804. /*---------------------------- END BCP CATALOG PROCS ------------------------*/
  23805.  
  23806. dump tran master with no_log
  23807. go
  23808.  
  23809.  
  23810.  
  23811.  
  23812. if (exists (select * from sysobjects
  23813.         where name = 'sp_configure' and type = 'P '))
  23814.     begin
  23815.         exec sp_configure 'allow updates',0
  23816.         reconfigure with override
  23817.     end
  23818. go
  23819.  
  23820. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  23821.  
  23822. go
  23823.  
  23824. if exists (select * from sysobjects where name = 'sp_check_objects'
  23825.             and type = 'P ')
  23826.     begin
  23827.         /* Only supported on 6.0 servers */
  23828.         print ''
  23829.         print 'Checking objects created by instcat.sql.'
  23830.  
  23831.         exec sp_check_objects 'catalog'
  23832.     end
  23833. go
  23834.  
  23835. print ''
  23836. print 'instcat.sql completed successfully.'
  23837. go
  23838.  
  23839. set quoted_identifier off
  23840. go
  23841.  
  23842. dump tran master with no_log
  23843. go
  23844. checkpoint
  23845. go
  23846. /**/
  23847.